ember.js 如何(如果可能的话)在Javascript过程或函数中调用Ember Action?

e0bqpujr  于 2022-11-05  发布在  Java
关注(0)|答案(1)|浏览(128)

我的问题是在同一个文件中定义的一个Ember动作作为Javascript过程,在Ember 2.1。
例如,如这里的ember代码所在的地方,我们想调用到myAction以外的“export”区域:

export default Component.extend({

  actions: {
    myAction() {
    }
  }
});  

function test(x) {
  CALL TO myAction HERE
  return(3x);
}
vlju58qv

vlju58qv1#

在现代的ember操作中,它们只是函数,所以你可以直接称它们为:

export default class MyComponent extends Component {
  myFunc() {
    this.myAction(); // this just works
  }

  @action
  myAction() {
  }
}

在旧的embers经典类中有send,或者你可以访问动作和.call它。

export default Component.extend({
  myFunc() {
    this.send('myAction'); // this calls the action
    this.actions.myAction.call(this); // this calls the action
  }
  actions: {
    myAction() {
    }
  }
});

相关问题