javascript - Ember.js 中 `this` 的基本使用
问题描述
我相当擅长 Javascript,但我很难理解 Emberthis
在某些情况下是如何处理上下文的。
我有一个组件控制器:
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
,或者相反。
这种行为似乎是必要的,因为组件事件属于操作 hash 之外。
那么我应该如何在actions
散列之外拥有事件方法,但仍然能够调用属于actions
散列内部的方法呢?
这似乎是 Ember 控制器中记录不充分的部分。也许我只是在这里遗漏了一些东西。任何帮助是极大的赞赏。
解决方案
要从actions
哈希外部调用操作,请使用this.send
this.send('onAccept');
更多信息可以在这里找到:send
https ://medium.com/ember-titbits/ember-send-and-sendaction-5e6ac9c80841sendAction
推荐阅读
- typescript - 如何用 jest 模拟打字稿界面?
- javascript - 如何将 Popover/Modal 及其背景限制在容器 div 内(以允许滚动)?
- c - 引擎盖下的阻塞和非阻塞文件描述符
- java - 在同一循环中将项目添加到多个数组中
- sql - MS ACCESS:表格和子表格完全链接?还是表格和未绑定的连续子表格?
- vespa - 有没有办法用 Vespa 执行加权 elementSimilarity?
- haskell - 在haskell函数的归纳步骤中检查字符串中的空格
- apache-flink - Flink 数据流到表
- webstorm - 如何为 WebStorm 中的多个项目在同一文件夹中定义环境变量?
- scala - Scala - 在自定义对象列表中查找最大值和值的总和