使用yield的Ember渲染组件

时间:2017-08-03 04:26:23

标签: javascript jquery ember.js

我的my-component1.js定义如下;

export default Ember.Component.extend({ 
prop1: 'abc'
})

相应的模板my-component1.hbs如下所示;

{{#my-yield-component2}}
    {{my-other-component3 field=(my-helper prop1)}}
{{/my-yield-component2}}

所以我想在my-yield-component2中显示/渲染my-other-component3(因为我正在使用yield)

my-yield-component2内,我有以下内容;

<div>My component with yield</div>
<div>{{yield}}</div>

我的问题是如何通过/访问&#34; prop1&#34;它实际上是在my-component1中定义的,但由于我使用的是yield,它将在my-yield-component2中呈现 所以我想知道如何通过&#34; prop1&#34; ?

1 个答案:

答案 0 :(得分:1)

prop1的{​​{1}}值传递给my-component1的方式没有任何问题。 my-other-component3模板文件中的上下文为my-component1本身;因此即使在my-component1内呈现prop1my-component1也会从my-other-component3传递到my-other-component3。请查看以下twiddle,其中说明了我到目前为止所解释的内容顺利进行。

关于从my-yield-component2传递到my-yield-component2的价值是另一个故事;你需要my-other-component3的地方......从yield的模板中将其传递给my-yield-component2,如下所示:

my-other-component3

我已经在twiddle之前的问题中提供了我上面解释的工作one