meteor - 铁路由器 : How do I send data to the layout?

标签 meteor iron-router

使用 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/

相关文章:

css - Meteor 包 accounts-ui-bootstrap-3 下拉菜单出现在屏幕外

ios - 'meteor build',cordova - ios 平台错误

javascript - Meteor 动态 url 显式模板查找路径代替

meteor - 自定义 Meteor 注册模板

javascript - Meteor.js : iron-router Router. 路线()不工作

javascript - meteor :仅返回集合内嵌套数组中的单个对象

javascript - 我们如何或可以通过 npm 和 Meteor 使用 Node 模块?

javascript - XMLHttpRequest 无法加载 - Meteor 和 FSCollection 没有 'Access-Control-Allow-Origin'

javascript - 使用 IronRouter/MeteorJS 在 Bootstrap Modal 中提交表单

javascript - Iron Router 未在产量中渲染模板