【问题标题】:emit event with arguments from child to parent with ember 3.6使用ember 3.6从孩子向父母发出带有参数的事件
【发布时间】:2019-05-21 07:28:40
【问题描述】:

我想从孩子向父母发出一个事件,但有一些论据。

子组件 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);
    }
  }
});

我在 console.log(orgId); 中得到 undefined 为什么?我错过了什么

谢谢!

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    您只需将organization.id 更改为id。我的意思是;您需要执行以下操作:

    {{child-component
      clicked=(action "favoriteClicked" value="id")
    }}
    

    您从带​​有组织的子组件发送事件;它只包含一个id;但不是organization.id;因此在父模板中传递的action 中的value 属性必须是id

    我为您准备了ember twiddle 来说明我提出的实际解决方案。希望对您有所帮助。

    【讨论】:

    • 是否可以添加多个参数?我正在尝试类似clicked=(action "favoriteClicked" value="id, favorites") 但避免使用id...再次感谢您!
    • @ntj34 AFAIK 您只能将一个属性名称指定为actionvalue 属性。我查看了官方指南,似乎我所知道的。
    • 再次感谢您:)
    猜你喜欢
    • 2018-01-22
    • 1970-01-01
    • 2017-03-07
    • 2019-11-21
    • 2015-09-14
    • 1970-01-01
    • 2018-06-06
    • 2022-11-15
    • 2018-10-05
    相关资源
    最近更新 更多