【问题标题】:Meteor #each @index passed to Dynamic Template?Meteor #each @index 传递给动态模板?
【发布时间】:2016-05-21 15:13:33
【问题描述】:

我正在尝试将项目的索引从 {{#each}} 循环传递到动态模板中,但我不知道如何将它送到那里(以一种干净的方式)。

当前代码:

{{#each item}}
{{Template.dynamic template=type data=this}}
{{/each}}

这样,{{@index}} 就无法在动态加载的模板中访问。

我也尝试过使用模板助手,但它似乎没有在上下文中跟踪索引。

{{#each item}}
{{Template.dynamic template=type data=itemData}}
{{/each}}

Template.items.helpers({
  itemData() {
    // can't access index in here
    return this;
  }
});

谁能建议我如何实现这一目标?

谢谢!

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    使用以下模式解决了这个问题:

    ... Template.Items
    {{#each items}}
    {{>Template.dynamic itemConfig @index}}
    {{/each}}
    
    Template.items.helpers({
      itemConfig(index) {
        const data = this;
        data.index = index;
    
        return {
          data,
          template: this.type //this.type is where im storing my template name
        };
      },
    });
    

    使用@index 作为辅助参数,然后 Blaze 使用该对象作为动态模板的配置!

    :)

    编辑:我找到了another solution。做同样的工作,我更喜欢它的外观。

    {{>Template.dynamic template=type data=(templateData @index)}}
    

    templateData 与之前的助手基本相同,但只是返回带有 index 属性的 data

    【讨论】:

      【解决方案2】:
      {{#each item}}
      {{Template.dynamic template=type index=@index}}
      {{/each}}
      

      您可以在动态模板中使用'index'来访问索引

      【讨论】:

        猜你喜欢
        • 2016-02-10
        • 2016-04-17
        • 2015-09-30
        • 2015-07-10
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-20
        相关资源
        最近更新 更多