【问题标题】:Meteor Iron:Route with Meteor 1.2Meteor Iron:使用 Meteor 1.2 的路线
【发布时间】:2015-10-04 06:36:45
【问题描述】:
【问题讨论】:
标签:
meteor
this
iron-router
【解决方案1】:
查看错误:
未捕获的类型错误:无法读取未定义的属性“通道”
我们可以看到它正在尝试读取未定义对象的属性“通道”。您代码中的这个对象是:
this.params
阅读iron:router docs,告诉我们:
当用户访问该 url 时,参数的实际值将作为属性存储在 this.params 在您的路由函数中。
但是在您提供的代码 sn-p 中:
Meteor.startup(function(){
Session.set('channel',this.params.channel);
});
您试图在 Meteor.startup 回调函数中访问 this.params,而不是 Router.route 函数。在这种情况下 this.params 是未定义的,正如错误告诉你的那样。
更改您的启动代码以匹配教程:
Meteor.startup(function() {
Session.set('channel', 'general');
});
在您的路由代码中,您可以像这样使用路由中的值:
Router.route('/:channel', function () {
Session.set('channel', this.params.channel);
this.render('messages');
});
在这种情况下 this.params 将返回一个对象,包括在路由路径中定义的通道属性。