我在尝试获取Reactive Var值时得到Uncaught TypeError: Cannot read property 'chapter' of null
。我正在使用Session做同样的事情并且它有效。所以我对我的错误感到困惑。也许价值是在错误的时间设定的?
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var'
import { Stories } from '/imports/collections/stories.js';
/* Story */
Template.story.onCreated(function(){
// Set the current chapter
this.chapter = new ReactiveVar(0);
this.page = new ReactiveVar(0);
});
Template.story.onRendered(function(){
/* Show the first fragment */
page.addEventListener( 'changed', function( event ) {
console.log( Template.instance().chapter.get() );
....
事件被触发,但章节的实例未在onCreated中设置。
我在这里做错了什么?
答案 0 :(得分:1)
试试这个:
Template.story.onRendered(function(){
let instance = Template.instance();
/* Show the first fragment */
page.addEventListener( 'changed', function( event ) {
console.log( instance.chapter.get() );