【问题标题】:How to create recursive hbs code in ember?如何在 ember 中创建递归 hbs 代码?
【发布时间】:2016-11-18 09:50:38
【问题描述】:

我有一个包含以下内容的 hbs 文件

//some ohter code
-------------------------------------
    {{#each items}}
            <li>
                {{name}}
                {{#if items}}
                    <ul>
                        //partial should go here
                    </ul>
                {{/if}}
            </li>
        {{/each}}
---------------------------------------------------

我想使用same section 递归替换//partial should go here。我怎样才能做到这一点?感谢任何帮助

【问题讨论】:

  • 听起来你想把它放在一个组件中

标签: recursion ember.js handlebars.js


【解决方案1】:

Ember 曾经有 paritalrenderinclude 模板标签,但他们正试图摆脱这些标签。 现在,可能最好的方法是利用组件。如果您使用的是 ember-cli,只需打开终端并输入 ember g component my-include。它会生成一个 *.js 和一个 *.hbs 文件(命名取决于您是否使用 pod)。 然后将要多次使用的模板代码放入新创建的 *.hbs 文件中,然后放入现有的模板代码中:

{{#if items}}
      <ul>
        {{my-include}}
      </ul>
{{/if}}

您可能想看看:https://guides.emberjs.com/v2.9.0/components/defining-a-component/ 了解更多信息

你也可以看看这个sample working twiddle

【讨论】:

  • 我同意你的看法。所以我刚刚在你的帖子中添加了一个小技巧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2017-09-05
  • 1970-01-01
  • 2012-10-08
相关资源
最近更新 更多