无法从流星 js 中的帮助程序获取反应性变量

Can't get reactive var from helper in meteor js

我正在尝试将反应变量的值写入流星模板:

Template.form.rendered = function() {
    this.color = new ReactiveVar();
    this.color.set('#333555');
};

然后我定义了一个 "currentColor" 帮助程序以在模板中使用以打印颜色:{{currentColor}}

Template.form.helpers({
    currentColor: fuction() {
        return Template.instance().color.get();
    }
});

但是没用; 所以我尝试添加几个 console.log:

Template.form.helpers({
    currentColor: fuction() {
        console.log(Template.instance());
        console.log(Template.instance().color);
    }
});

奇怪的是,第一个控制台日志显示 Blaze.TemplateInstance,颜色为 属性:

Blaze.TemplateInstance {...}
    color: ReactiveVar
        curValue: "#333555"
        dep: Tracker.Dependency
        equalsFunc: undefined
    ...

但是第二个日志是"undefined"; 有人可以帮助我理解这一点吗?

您应该将 ReactiveVar 定义放在 template.created() 函数中,而不是 template.rendered() 函数中。