我想从 child 到 parent 发出一个事件,但有一些争论。
子组件 javascript
import Component from '@ember/component';
export default Component.extend({
tagName: 'li',
actions: {
favoriteWasClicked() {
const organization = this.get('organization');
// organization.id value equal to 'facebook' here in my app
// we're gonna send its id to parent component "orgs" like an argument of clicked method
this.clicked(organization);
}
}
});
父组件.hbs模板
{{child-component
clicked=(action "favoriteClicked" value="organization.id")
}}
父组件javascript Controller 文件
import Controller from '@ember/controller';
export default Controller.extend({
actions: {
favoriteClicked(orgId) {
console.log(orgId);
}
}
});
我在 console.log(orgId); 中得到 undefined 为什么?我错过了什么
谢谢!
最佳答案
您只需将 organization.id
更改为 id
。我的意思是;您需要执行以下操作:
{{child-component
clicked=(action "favoriteClicked" value="id")
}}
您从具有组织的子组件发送事件;它只包含一个 id
;但不是 organization.id
;因此在父模板中传递的 action
中的 value
属性必须只是 id
。
我准备了一个 ember twiddle为您说明我在行动中提出的解决方案。希望对您有所帮助。
关于javascript - 使用 ember 3.6 发出带有从子到父参数的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874375/