Javascript 将函数传递到角度方向上的其他对象时出现问题

Javascript 将函数传递到角度方向上的其他对象时出现问题,javascript,angular,leaflet,Javascript,Angular,Leaflet,我在将函数传递给角度中的其他对象时遇到问题。具体地说,我创建了一个函数generateTile(coords),它填充一个平铺,然后将该平铺提供给传单。此函数位于MapComponent方法中。我想我理解为什么这是一个问题,因为这指的是不同的上下文。然而,我不知道如何解决这个问题 generateTile(coords) { ... return image; } private initMap(): void { this.map = L.map('map', {

我在将函数传递给角度中的其他对象时遇到问题。具体地说,我创建了一个函数
generateTile(coords)
,它填充一个平铺,然后将该平铺提供给传单。此函数位于MapComponent方法中。我想我理解为什么这是一个问题,因为
指的是不同的上下文。然而,我不知道如何解决这个问题

generateTile(coords) {
    ...
    return image;
}

private initMap(): void {
    this.map = L.map('map', {
        crs: L.CRS.Simple,
        center: [0, 0],
        zoom: 5
    });

    L.TileLayer.CustomMap = L.TileLayer.extend({
        getTileUrl: function(coords) {
            var img = this.generateTile(coords);
            return img.src;
        },
        getAttribution: function() {
            return "<a href='https://example.com'>Example</a>"
        }
    });
...
}
generateTile(坐标){
...
返回图像;
}
私有initMap():void{
this.map=L.map('map'{
crs:L.crs.Simple,
中间:[0,0],
缩放:5
});
L.TileLayer.CustomMap=L.TileLayer.extend({
getTileUrl:函数(coords){
var img=此.generateTile(coords);
返回img.src;
},
getAttribute:function(){
返回“”
}
});
...
}

使用箭头函数,这样您就不会为此创建范围。例如,您可以阅读更多关于
这个
如何在JavaScript中工作的信息

  getTileUrl: (coords) => {
                var img = this.generateTile(coords);
                return img.src;
            };

也许问题还不清楚,但问题就在于这个。我的意图是使用MapComponent的
this
,但是匿名函数getTitleUrl中的
this
是不同的。