问题描述
使用Iron Router,我了解如何为模板设置数据.但是,如何将数据发送到全局定义的布局?
Using Iron Router, I understand how to set data for a template. But how do I send data to a layout that was globally defined?
我通过以下方式设置路由器布局:
I set the router layout by:
Router.configure({ layoutTemplate: 'NAME' })
这将设置我所有路线的布局.
This will set the layout for all my routes.
不过,我想从自己的路线中将数据发送到布局模板.
However from my individual routes, I'd like to send data to the layout template.
推荐答案
布局使用在route选项中用data
定义的数据上下文.以下是Iron Router 文档的摘录:
The layout uses the data context defined with data
in the route option. Here is an excerpt from 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
设置布局的数据上下文,如下所示:
We can also set the data context of the layout with this.layout
like this:
Router.route('/routeName', function () {
this.layout('layoutName', {
data: function() {
return CollectionName.find();
}
});
});
此外,我们可以像这样引用现有的layoutTemplate
选项:
In addition, we can refer to an existing layoutTemplate
option like this:
Router.route('/routeName', function () {
this.layout(this.lookupOption('layoutTemplate'), {
data: function() {
return CollectionName.find();
}
});
});
这篇关于Iron Router:如何将数据发送到布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!