我有一个可观察的订阅,所以当它完成时我需要它从这个类调用一个函数,问题是“this”关键字指的是订阅而不是类范围。 代码:
export class GoogleMapComponent{
Position:object;
constructor(public MapFunctionsProvider: MapFunctionsProvider) {
let posObservable = this.MapFunctionsProvider.getPosition();
posObservable.subscribe(data =>{
this.Position = data;
this.createMap();// this keyword refers to the subscription
});
function createMap(){
console.log('run')
}
}
如何在不声明“this”关键字的新变量的情况下调用 createMap() ?
最佳答案
在构造函数外部实现该函数。因为 this
引用 GoogleMapComponent
类
export class GoogleMapComponent {
Position: object;
constructor(public MapFunctionsProvider: MapFunctionsProvider) {
let posObservable = this.MapFunctionsProvider.getPosition();
posObservable.subscribe(data => {
this.Position = data;
this.createMap(); // this keyword refers to the subscription
});
}
createMap(): any {
console.log('run')
}
}
关于angular - subscribe 中的 Angular 2 "this"关键字如何引用该类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858770/