是否可以将函数传递给 Ember.js 组件以将其作为动作回调执行?

Is it possible to pass a function to an Ember.js component to execute it as an action callback?

我想知道是否可以将函数作为 Ember 组件的参数传递

// component.hbs
{{component fun=fun}}

然后,从组件中的一个动作,调用同一个函数:

// component.js
actions: {
  fun2() {
     let fun = this.get('fun');
     fun();
  }
}

查看以下内容twiddle。你问的是有效的并且有效。但一般来说,您可以将函数传递给带有 action 帮助器的组件,并在 actions 定义中定义您的操作。我能说的是,这是将函数传递给组件并从组件内部触发操作(调用函数)的常用方法。

我的-component.js

import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
    buttonClicked() {
      let foo = this.get('foo');
      foo();
    }
  }
});

application.js

import Ember from 'ember';

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',

  foo() {
    alert('hi there');
  }
});

application.hbs

<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{my-component foo=foo}}
<br>
<br>

我的-component.hbs

<button onclick={{action 'buttonClicked'}}>Press</button>