【问题标题】:Meteor - I can’t use ReactiveVar in a blaze helperMeteor - 我不能在 blaze helper 中使用 ReactiveVar
【发布时间】:2018-03-24 06:50:54
【问题描述】:

我想在我的模板 onCreated 方法中初始化一个 ReactiveVar 并将它传递给一个助手:

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './record-infos-page.html';
import './record-infos-page.scss';

Template.Record_infos_page.onCreated(function recordInfosPageOnCreated() {
  this.checkedVariablesIds = new ReactiveVar(['HkcEjZjdxbobbNCZc', 'uTmJKTfoWFFfXPMwx']);
  console.log(1, this.checkedVariablesIds.get());
});

Template.Record_infos_page.helpers({
  checkedVariablesIds() {
    const vars = Template.instance().checkedVariablesIds.get();
    console.log(2, vars);
    return vars;
  },
});

console.log 结果是:

1 ['HkcEjZjdxbobbNCZc', 'uTmJKTfoWFFfXPMwx']
2 undefined

为什么我的助手中有 undefined?

谢谢!

【问题讨论】:

  • 由于这在论坛中已被解决为一个简单的错字,您可能应该删除它,因为错字问题与 SO 无关
  • 作为参考,Meteor 论坛交叉帖子:forums.meteor.com/t/…“我的错,我的子模板中有另一个助手删除了我的 reactiveVar。”

标签: meteor meteor-blaze


【解决方案1】:

您可以使用instance.autorun 响应式地获取模板中变量的更改:

您可以在 onCreated 或 onRendered 回调中使用 this.autorun 响应式更新 DOM 或模板实例。

来自Blaze documentation

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './record-infos-page.html';
import './record-infos-page.scss';

Template.Record_infos_page.onCreated(function() {
  const instance = this;
  instance.autorun(function() {
    if (!instance.checkedVariablesIds) {
      instance.checkedVariablesIds = new ReactiveVar(['HkcEjZjdxbobbNCZc', 'uTmJKTfoWFFfXPMwx']);
      console.log(1, instance.checkedVariablesIds.get());
    }
  });

});

Template.Record_infos_page.helpers({
  checkedVariablesIds() {
    const vars = Template.instance().checkedVariablesIds.get();
    console.log(2, vars);
    return vars;
  },
});

【讨论】:

  • 是的,但是我的第二个控制台日志仍然显示2 undefined。它应该显示反应变量的内容。
  • 那么请发布整个模板以及 html 和 css 文件以及您用来完全重现此问题的软件包版本。在我的测试项目中,这没有任何问题。
  • 哦,好吧,如果它不能为您重现,那是个好消息。我会更深入地调查谢谢 ;)
猜你喜欢
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 2016-03-24
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多