【问题标题】:Yield complete block in ember在余烬中产生完整的块
【发布时间】:2017-03-10 06:45:32
【问题描述】:

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

有人可以解释一下吗?

【问题讨论】:

  • 您是否尝试过 ember-twiddle.com,它非常适合学习 ember 和重现特定问题并要求澄清。你有更多机会得到你真正需要的东西。
  • 谢谢,下次我会用它

标签: javascript ember.js handle yield


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回答。那么这是一种安全,意味着我只能使用在收益中定义的值和模型吗?
  • 抱歉,刚看到这个问题。不是真正的安全问题。组件背后的设计理念是它们没有任何上下文——它们不知道当前的模型。为了确保可重用性,所有数据都必须从路由注入到组件中。
猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多