问题描述
如果我有一个名为 my-scope
的组件,仅定义为:
if I have a component called my-scope
which is defined only as:
export default Ember.Component.extend({
foo: 'bar'
})
然后我使用模板中的组件,如下所示:
and then I use the component in a template, like so:
{{#my-scope}}
{{foo}}
{{/my-scope}}
如何达到周边块组件的范围?我试过:
How can I reach the scope of the surrounding block component? I have tried:
-
{{foo}}
-
{{component.foo}}
-
{{view.foo}}
{{foo}}
{{component.foo}}
{{view.foo}}
也许这是我需要使用视图的情况?希望不是,但也许是这样的情况。
Maybe this is a case where I need to use a view? Hoping not but maybe that is the case.
推荐答案
您可以使用 _view.parentView
访问<$组件内的c $ c> foo 属性。 (因为在 _view
中有一个下划线,对我来说,这意味着它不是公共API,所以他们可能会在将来摆脱它 - 但它现在在... :)
You can use _view.parentView
to access the value of foo
property inside the component. (Because there is an underscore in _view
, to me that means that it's not public API, so they might get rid of it in the future - but it's around for now :))
如下所示:
{{#x-test}} Inside c1: {{ _view.parentView.foo.name }} {{/x-test}}
工作演示
这篇关于访问模板块内的组件范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!