簡體   English   中英

Ember.js中this的基本用法

[英]Basic use of `this` in Ember.js

我相當精通Java語言,但是很難理解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的散列, 或者反過來。

而且這種行為似乎是必需的,因為組件事件屬於actions hash之外

那么,我應該如何在actions哈希之外具有事件方法,但仍然能夠調用在actions哈希內部屬於的方法呢?

在Ember中,這似乎是控制器中記錄很少的一部分。 也許我只是在這里想念東西。 任何幫助是極大的贊賞。

要從actions哈希之外調用動作,請使用this.send

this.send('onAccept');

有關sendsendAction更多信息, send參見以下網址https : sendAction

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM