Ember.js 中 `this` 的基本用法

Basic use of `this` in Ember.js

我相当擅长 Javascript,但我很难理解 Ember 在某些情况下如何处理 this 上下文。

我有一个组件控制器:

import Component from '@ember/component';

export default Component.extend({
  keyPress(e) {
    // here I want to call a method in the `actions` hash
    this.get('onAccept')
  },

  actions: {
    onAccept() {
      console.log('action accepted!')
    }
  }
}

虽然我每次 运行 都会收到以下错误:

Uncaught TypeError: this.get(...) is not a function

当我在 actions 散列之外有一个方法需要访问 actions 散列内的函数时,这似乎总是发生,反过来。

而且这种行为似乎是必要的,因为组件事件属于 outside of the actions hash

那么我应该如何在 actions 散列之外拥有事件方法,但仍然能够调用属于 actions 散列内部的方法?

这似乎是 Ember 中控制器的一个记录不完整的部分。也许我只是在这里遗漏了一些东西。非常感谢任何帮助。

要从 actions 哈希之外调用操作,请使用 this.send

this.send('onAccept');

有关 sendsendAction 的更多信息可在此处找到:https://medium.com/ember-titbits/ember-send-and-sendaction-5e6ac9c80841