在我的Ember应用程序中,我有一个属性,我在父组件的init()
方法中创建。我想把它传给孩子们。
// app/component/parent.js
import Ember from 'ember';
import ParentProperty from 'somewhere';
export default Ember.Component.extend({
init() {
this._super(...arguments);
let model = this.get('model');
let moreData = this.get('moreData');
this.parentProperty = new ParentProperty(model, moreData);
}
click(e) {
this.get('moreData').doSomthing();
}
});
// app/component/application.hbs
{{#parent model=model moreData=moreData}}
// Here, I want to pass the `parentProperty` created
// in the init of the parent component.
{{child parentProperty=parent.parentProperty}}
{{/parent}}
我知道我可以使用{{with}}
帮助器来创建一个ParentProperty,包装父和子,然后将它传递给它们,但这不是我想要做的。这可能吗?
答案 0 :(得分:1)
正如@Lux建议的那样,我可以使用yield helper实现这一点。
// app/component/parent.js
import Ember from 'ember';
import ParentProperty from 'somewhere';
export default Ember.Component.extend({
init() {
this._super(...arguments);
let model = this.get('model');
let moreData = this.get('moreData');
this.parentProperty = new ParentProperty(model, moreData);
}
click(e) {
this.get('moreData').doSomthing();
}
});
// app/component/parent.hbs
{{yield parentProperty}}
// app/component/application.hbs
{{#parent model=model moreData=moreData as |parentProperty|}}
{{child parentProperty=parentProperty}}
{{/parent}}