我在尝试获取反应变量值时遇到Uncaught TypeError: Cannot read property 'chapter' of null
。我正在对会话做完全相同的事情,它可以工作。所以我对自己出了什么问题感到困惑。也许值设置在错误的时间?
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() );
....
正在触发事件,但未在 onCreate 中设置章节的实例。
我在这里做错了什么?
试试这个:
Template.story.onRendered(function(){
let instance = Template.instance();
/* Show the first fragment */
page.addEventListener( 'changed', function( event ) {
console.log( instance.chapter.get() );