使用 Ember 3.6 发出带有从子项到父级的参数的事件



我想从子级到父级发出一个事件,但有一些参数。

子组件 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

我为您准备了一个余烬,以说明我在行动中提出的解决方案。 希望对您有所帮助。

最新更新