我的问题是我有两个相似的路径,第一个路由器等待我的订阅并渲染整个模板,但第二个路由器立即渲染而没有加载,并且传递的数据导致错误(因为没有订阅的集合)然而)。 我将我的代码粘贴到此处,第二个代码由于传递的模板和数据而有所不同,但其余部分实际上是相同的。 我刚刚开始使用铁路由,也许有人可以告诉我错误在哪里?
Router.map(function() {
this.route('/', {
onBeforeAction: function() {
if (Meteor.user()) {
if (Meteor.user().firstLogin)
this.render("firstLogin");
else
Router.go('/news');
} else {
this.render("start");
}
},
waitOn: function() {
return Meteor.subscribe('allUsers');
},
onAfterAction: function() {
document.title = "someTitle";
},
loadingTemplate: "loading",
});
this.route('users',{
path:'/user/:_id',
layoutTemplate: 'secondLayout',
yieldTemplates: {
'template1': {to: 'center' },
'template2': {to: 'top' },
'template3': {to: 'left' },
'template4': {to: 'right' },
},
waitOn: function(){
return Meteor.subscribe("allUsers");
},
data: function(){
return Meteor.users.findOne({_id:String(this.params._id)});
},
loadingTemplate: "loading",
});
});
最佳答案
您正在旧版本中使用iron-router。如果你刚刚开始的话。我建议您使用新的 api。在这种情况下,您可以使用 this.ready()
来检查订阅是否完成
以下是 official guide 中的示例
Router.route('/post/:_id', function () {
// add the subscription handle to our waitlist
this.wait(Meteor.subscribe('item', this.params._id));
// this.ready() is true if all items in the wait list are ready
if (this.ready()) {
this.render();
} else {
this.render('Loading');
}
});
关于javascript - 路由器不等待订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044613/