有一个简单的解决方案和一个硬编码的解决方案来实现这一点。
简单的解决方案它使用accounts-ui 包,并且只需使用
{{> loginButtons}} helper 插入 HTML,这实际上具有您想要的预期行为。
还有艰难的路
存在两种可能的解决方案,使用简单的 jQuery 代码和 Css。像这样。
jQuery 和 CSS 解决方案
使用与示例相同的 HTML,使用这 2 个事件处理程序。
Template.example.events({
'click #aRegister':function(){
$(".login").css('visibility', 'hidden');
$(".register").css('visibility', 'visible');
},
'click #aLogin':function(){
$(".register").css('visibility', 'hidden');
$(".login").css('visibility', 'visible');
}
})
还有这个.css
.register{
visibility: hidden;
}
这里是MeteorPad
Meteor Way(使用 Sessions 和#if)
首先使用相同的 2 个事件,我们使用一些 Sessions 变量,将会话的值设置为 true/false,如下所示。
Template.example.events({
'click #aRegister':function(){
Session.set('showRegister',true);
},
'click #aLogin':function(){
Session.set('showRegister',false);
}
})
现在在 HTML 中我们使用 {{#if}}
<template name="example">
{{#if showTheRegisterDiv}}
<!-- Here if the Session returns its == true this part of the template will be rendered -->
<div class="register">
<h1> Register. </h1>
<a href="#" id="aLogin">Log in</a>
</div>
{{else}}
<!-- or if its == false we render this part -->
<div class="login">
<h1> Log in. </h1>
<a href="#" id="aRegister">Create an account</a>
</div>
{{/if}}
</template>
多亏了这个模板助手,这终于奏效了。
Template.example.helpers({
showTheRegisterDiv:function(){
return Session.get('showRegister')
}
})
这里是MeteorPad