angular - X 不是 onClick 中的函数
问题描述
我有一个基本循环:
markers.forEach((marker) => {
marker.addListener('click', this.openInfoWindow(marker));
})
我有一个 TypeScript 函数声明为:
openInfoWindow(marker) { }
我正进入(状态:
zone-evergreen.js:172 Uncaught TypeError: this.openInfoWindow is not a function at some.component.ts:147 at Array.forEach () at Di。(some.component.ts:145) at ne.H at Object。.N.trigger at re at Di。.O.set at Di.setZoom at map.js:29 at xw.T (map.js:41)
为什么会出现这种情况?我的click
调用有什么问题会导致 TypeScript 看不到这个函数?
解决方案
看起来它失去了范围
试试这个
markers.forEach((marker) => {
marker.addListener('click', () => this.openInfoWindow(marker));
})
推荐阅读
- python - Python 3.4.3 - 多项目追加和列表扩展
- java - LibGDX:AssetManager 有限制吗?
- rest - microsoft sharepoint 使用 REST API 访问文档返回 404
- php - laravel 服务提供者对 laravel 有什么用
- api - 如何禁用 unicode id creaton 并对注册 API 上的 id 创建设置长度限制
- php - php如何从数据库中创建所有表的下拉选择输入?
- javascript - VueJs v-on:event 和 this.$on(event, handler) 有什么区别?
- javascript - Stripe Payment 仅适用于我的 LocalHost,但不适用于 Heroku 服务器
- java - Java-多线程以提高将数据插入数据库的性能
- python-3.x - 如何检查文件夹中的文件以及检查子文件夹中的文件