Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模板帮助程序在session.set之后未更新_Javascript_Meteor - Fatal编程技术网

Javascript 模板帮助程序在session.set之后未更新

Javascript 模板帮助程序在session.set之后未更新,javascript,meteor,Javascript,Meteor,此Meteor代码需要根据会话.get('headerLabel')更改headerLabel的值,当在不同的客户端文件中设置时,它不会更新模板显示 为什么以及如何修复它?谢谢 //client/header.js Template.header.helpers({ headerLabel:函数(){ 让userId=Meteor.userId(); 如果(!userId){ Session.set('headerLabel','Please login'); }否则{ Meteor.subsc

此Meteor代码需要根据会话.get('headerLabel')更改
headerLabel
的值,当在不同的客户端文件中设置时,它不会更新模板显示

为什么以及如何修复它?谢谢

//client/header.js
Template.header.helpers({
headerLabel:函数(){
让userId=Meteor.userId();
如果(!userId){
Session.set('headerLabel','Please login');
}否则{
Meteor.subscribe(“标题标签”);
让label=HeaderLabelCol.findOne({userId:userId}).headerLabel;
Session.set('headerLabel',标签);
}
返回{headerLabel:Session.get('headerLabel')};
}
});
//client/lib.js
实用工具=(函数(){
返回{
toast:功能(headerMsg){
const temp=Session.get('headerLabel');
Session.set('headerLabel',headerMsg);
setTimeout(()=>{Session.set('headerLabel',temp)},2000);
}
}
}());

☰
{{headerLabel.headerLabel}
⋮
这是从客户端的其他文件调用的
utility.toast('error entries')

助手函数应该只获取模板所需的数据,它们不应该操纵应用程序的状态。特别是,您调用的
Meteor.subscribe
在每次重新提交模板时都会创建一个新的订阅句柄。这是一个严重的泄漏

相反,您应该将逻辑代码移动到
onCreated
onRendered
onestroyed
方法

Template.header.onCreated(function() {
  this.subscribe('headerLabel');
  this.autorun(function() {
    let userId = Meteor.userId();
    if (userId) {
      let label = HeaderLabelCol.findOne({userId: userId}).headerLabel;
      Session.set('headerLabel', label);
    } else {
      Session.set('headerLabel', 'Please login');
    }
  });
});

Template.header.helpers({
  headerLabel: function () {
    return Session.get('headerLabel');
  },
});