我想从子级到父级发出一个事件,但有一些参数。
子组件 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 控制器文件
import Controller from '@ember/controller';
export default Controller.extend({
actions: {
favoriteClicked(orgId) {
console.log(orgId);
}
}
});
我在控制台.log(orgId)中未定义;为什么?我错过了什么
谢谢!
您只需将organization.id
更改为 id
.我的意思是;您需要执行以下操作:
{{child-component
clicked=(action "favoriteClicked" value="id")
}}
您从带有组织的子组件发送事件;该组织只包含一个id
;但不包含organization.id
;因此它必须只是在父模板中传递的action
中的value
属性id
。
我为您准备了一个余烬,以说明我在行动中提出的解决方案。 希望对您有所帮助。