【问题标题】:Meteor Iron Router load template based on dynamic nameMeteor Iron Router 基于动态名称加载模板
【发布时间】:2015-01-04 21:57:52
【问题描述】:

我想使用 Iron-router 加载一个流星模板,但我正在加载的模板需要是动态的,我尝试了几种不同的方法,但都没有奏效。

我的路由器

Router.route('/forms/add-form/:template', {
  name: 'addForm',
  layoutTemplate: 'layoutApp',
  waitOn: function() {
    return Meteor.subscribe('producersList');
  },
  data: function() {
    return Producers.find();
  }
}); 

路由器去

Router.go('addForm', {template: this.template});

网址很好,我第一次尝试在我的路由器中使用它

name: this.params.template,

但这不起作用我现在在我的 addForm 模板中尝试以下操作

{{> UI.dynamic template=formToLoad data=myDataContext}}

formToLoad:function(){
    console.log('aaaa ' + this.template);
}
});

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    您可以在路由中将数据传递到模板中:

    Router.route('/forms/add-form/:template', {
      name: 'addForm',
      layoutTemplate: 'layoutApp',
      waitOn: function() {
        return Meteor.subscribe('producersList');
      },
      data: function() {
        return {
          producers: Producers.find(),
          formToLoad: this.params.template //here pass the template name
        }
      }
    });
    

    在你的模板中:

    <template name="addForm">
      {{> Template.dynamic template=formToLoad}}
    </template>
    

    现在如果我们运行:

    Router.go('addForm', {template: 'someTemplateName'});
    

    它应该加载名为“someTemplateName”的模板。使用 camelCase 语法作为模板名称,因为当您为模板定义帮助程序或事件时,“some-template-name”会出现语法错误。

    【讨论】:

    • 感谢 fun 在我的代码中的其他地方做同样的事情。
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多