在 meteor 应用程序中,我试图创建一个页面,如果您按一个按钮,它会显示一个模板,然后如果您按另一个按钮,它会隐藏相同的模板。我尝试使用两个带有监听器的按钮来实现此功能,这两个按钮将 bool 值(称为切换)更改为 true 或 false。辅助方法应将 true 或 false 值返回给 if 语句,以便可以显示 anotherTemplate,但我收到此错误:
Uncaught ReferenceError: toggle is not defined at object.clickDataActionShow
似乎切换超出了范围?或者一些完全不同的东西,我无法理解。
值得一提的是,自动发布已被删除,因此是否有可能是发布/订阅问题?
无论哪种方式,这里都是我的代码:
list.html
<Template name="list">
<div>
<button class="btn btn-info" data-action="show" id="show">Show</button>
<button class="btn btn-danger" data-action="hide" id="hide">Hide</button>
{{ #if toggleGet }}
{{> anotherTemplate}}
{{ /if }}
</div>
</Template>
list.js
Template.list.created=function(){
this.toggle = new ReactiveVar(true);
};
Template.list.helpers(
{
toggleGet: function(){
return Template.instance().toggle.get();
}
}
);
Template.list.events(
{
//Sets toggle to true, shows anotherTemplate
'click [data-action="show"]': function(event, template) {
template.toggle.set(true);
},
//Sets toggle to false, hides anotherTemplate
'click [data-action="hide"]': function() {
template.toggle.set(false);
}
}
);
任何帮助将不胜感激,谢谢:)。
最佳答案
通过 onCreated 更改您创建的方法,它应该可以工作:)
Template.list.onCreated(function() {
this.toggle = new ReactiveVar(true);
})
关于javascript - meteor 未捕获引用错误 : variable is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067679/