在 ember 中产出完整区块

Yield complete block in ember

我想知道如何生成传递给我的组件的完整块。 我已经找到这个 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}} 中呈现...

有人能给我解释一下吗?

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

{{#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