使用 ember 3.6 发出事件,参数从 child 到 parent
emit event with arguments from child to parent with ember 3.6
我想发出一个从 child 到 parent 的事件,但有一些参数。
Child分量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);
}
}
});
Parent 组件.hbs 模板
{{child-component
clicked=(action "favoriteClicked" value="organization.id")
}}
Parent组件javascript控制器文件
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 来说明我在行动中提出的解决方案。希望对您有所帮助。
我想发出一个从 child 到 parent 的事件,但有一些参数。
Child分量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);
}
}
});
Parent 组件.hbs 模板
{{child-component
clicked=(action "favoriteClicked" value="organization.id")
}}
Parent组件javascript控制器文件
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 来说明我在行动中提出的解决方案。希望对您有所帮助。