使用 Iron Router,我了解了如何为模板设置数据。但是如何将数据发送到全局定义的布局?
我通过以下方式设置路由器布局:
Router.configure({ layoutTemplate: 'NAME' })
这将为我的所有路线设置布局。
但是,从我的个人路线来看,我想将数据发送到布局模板。
最佳答案
布局使用路由选项中的data
定义的数据上下文。以下是 Iron Router documentation 的摘录:
Router.route('/routeName', {
...
// A data function that can be used to automatically set the data context for
// our layout. This function can also be used by hooks and plugins. For
// example, the "dataNotFound" plugin calls this function to see if it
// returns a null value, and if so, renders the not found template.
data: function () {
return Posts.findOne({_id: this.params._id});
},
...
}
我们还可以使用 this.layout
设置布局的数据上下文,如下所示:
Router.route('/routeName', function () {
this.layout('layoutName', {
data: function() {
return CollectionName.find();
}
});
});
此外,我们可以引用现有的 layoutTemplate
选项,如下所示:
Router.route('/routeName', function () {
this.layout(this.lookupOption('layoutTemplate'), {
data: function() {
return CollectionName.find();
}
});
});
关于meteor - 铁路由器 : How do I send data to the layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406970/