我从一个特定页面调用的提供程序中有一个Observable.timer
。我还将一个函数传递给提供者 Observable,当它完成倒计时时,它将调用位于初始页面中的函数。但是在计时器结束后我收到错误:
注意:我在页面内导入了 View Controller 但没有提供者(如果我尝试在提供程序中导入 View Controller,我会收到另一个错误)
错误
取消订阅时发生 1 errors:↵1)TypeError:无法读取未定义的属性'viewCtrl'
页
newTimer() {
this.countDown = this.timerProvider.newTimer(this.endTimer); //pass function below
}
endTimer() {
const indexModal = self.viewCtrl.index;
// then we remove it from the navigation stack
//this.navCtrl.remove(2);
self.navCtrl.push(WinnersPage, {
gameId: self.gameId
}).then(() => {
});
console.log('timer ENDED');
}
**** PROVIDER
countDown: any;
counter = 1*100;
tick = 1000;
constructor(public http: HttpClient) {
//console.log('Hello TimerProvider Provider');
}
newTimer(endTimer) {
return Observable.timer(0, this.tick)
.take(this.counter).map(() => --this.counter)
.finally(() => endTimer());
}
1 回答
你可以使用简单,
要么:
由于你从
another function
调用function
,****的引用在endTimerfunciton
中不可用所以,将
component
this
分配给global variable self
并在endTimer function
中使用它试试这个,