stenciljs - 事件发出错误
问题描述
所以我有这个代码 -
@Component({
tag: "app-home",
styleUrl: "app-home.css"
})
export class AppHome {
@Event()
myEvent: EventEmitter;
callEv() {
this.myEvent.emit("hello");
}
render() {
return (
<div class="app-home">
<button onClick={this.callEv}>Hello</button>
</div>
);
}
}
单击按钮时,我在控制台中收到此错误-
未捕获的类型错误:无法读取未定义的属性“发出” 在 HTMLButtonElement.callEv
知道为什么吗?
解决方案
在callEv
方法内部,您使用this
关键字来调用事件发射器服务。但是在模板内部,上下文不同,因此this
不包含事件发射器服务,这会导致您的错误。
您需要做的是将当前上下文绑定到该方法。将此添加到您的课程中:
constructor() {
this.callEv = this.callEv.bind(this);
}
推荐阅读
- python - 使用python的列表中的元素总和
- javascript - 防止在模板文字中转义反斜杠
- c# - Ajax.beginform 将空值发布到控制器
- python - Dataframe python将一些值从水平重塑为垂直
- python - 在 Eclipse 中使用 Pydev 自动创建类
- python - 下载同名的不同文件 一次又一次地获取上一个文件
- java - Apache 轴与 java 11 的兼容性
- python - 哪个更有效 dict.get("1", "default val") 或者 if else 检查
- c# - Selenium C# 在网页中填充 Windows 安全性
- tensorflow - Keras 指标问题