在余烬中产生完全阻挡

时间:2016-10-27 14:18:39

标签: javascript ember.js handle yield

我想知道如何将完整的块传递给我的组件。 我已经找到了https://guides.emberjs.com/v2.9.0/components/block-params/ 但我不明白为什么会有

//my-component.hbs
{{#if hasBlock}}
  {{yield post.title}}
  {{yield post.body}}
  {{yield post.author}} ...

为什么我必须说出我想要的东西?这没有任何意义,因为无论我在那里做什么,我都希望得到(显示)我传递给组件的整个块。

所以我只尝试使用yield:

//my-component.hbs
{{#if hasBlock}}
  {{yield}} ...

以这种方式使用组件:

//myroute.hbs
{{#my-component car=model}}
  {{car.name}} - {{car.color}}
{{/my-component}}

这不起作用,但我预计'car.name - car.color'将在组件的{{yield}}中呈现......

有人可以解释一下吗,请问?

1 个答案:

答案 0 :(得分:0)

我承认,这有点令人困惑。发生的事情是组件正在产生(返回)您可以在块中使用的值。尝试这样的事情:

{{#my-component car=model as |car|}}
    {{car.name}} - {{car.color}}
{{/my-component}}

然后在你的组件中:

{{#if hasBlock}}
    {{yield car}}
{{else}}
    default no-block template goes here...
{{/if}}

这是一个有效的工作:https://ember-twiddle.com/9a0efb92ebea6f206236a32e3c3e6053?openFiles=templates.index.hbs%2Ctemplates.components.my-component.hbs