[英]Session.get does not set variable in Meteor.js
fruit
是模板帮助器Template.fruits.name
和Template.fruits.nick
之间共享的变量,以防止在每个单独的帮助器函数中重复代码。
但是,当change #fruit-selector
事件处理程序触发时,即使共享变量fruit
由Session.get('fruit')
设置,也不会更改。
如果我希望它是反应性的,这是否意味着我不能使用这种声明fruit
变量的方法?
main.js
(function() {
// Set default fruit
if(!Session.get('fruit')) {
Session.set('fruit', 'apple');
}
var fruit = Session.get('fruit');
Template.fruits.name = function() {
return fruit;
};
Template.fruits.nickname = function() {
return fruit + 'y';
};
Template.fruits.name2 = function() {
return Session.get('fruit');
};
Template.fruits.events({
'change #fruit-selector': function(e) {
Session.set('fruit', e.target.value);
console.log('fruit: ' + fruit)
}
});
}());
您必须在模板助手中使用Session.get('fruit')
,否则它们将不会响应更改( fruit
不是反应性数据源,它已从反应性数据源获得了价值)。
PS使用Session.setDefault('fruit', 'apple')
代替您的方法。
试试这个(您是说Template.fruit.nickname还是Template.fruits.nickname?):
(function() {
// Set default fruit
if(!Session.get('fruit')) {
Session.set('fruit', 'apple');
}
Template.fruits.name = function() {
return Session.get('fruit');
};
Template.fruit.nickname = function() {
return Session.get('fruit') + 'y';
};
Template.fruits.name2 = function() {
return Session.get('fruit');
};
Template.fruits.events({
'change #fruit-selector': function(e) {
Session.set('fruit', e.target.value);
console.log('fruit: ' + Session.get('fruit'))
}
});
}());
看起来,会话不再是默认流星程序包的一部分。 转到命令提示符并运行“流星添加会话”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.