首页 > 解决方案 > 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 看不到这个函数?

标签: angulartypescript

解决方案


看起来它失去了范围

试试这个

markers.forEach((marker) => {
  marker.addListener('click', () => this.openInfoWindow(marker));
})


推荐阅读