我正在使用Ionic3 + angular开发移动应用程序。我被一个奇怪的行为所困扰,这个"这个"上下文。
下面的是打字稿中的代码"
import { Component } from '@angular/core';
import { Events, IonicPage, NavController, NavParams, Popover, PopoverController } from 'ionic-angular';
import { Cons } from '../../providers/cons';
import { TransLoc } from '../../providers/trans-loc';
import { Util } from '../../providers/util';
@IonicPage()
@Component({
selector: 'page-location',
templateUrl: 'location.html'
})
export class LocationPage {
loc: TransLoc;
//handlers
private _setLoc: (locs: TransLoc[]) => void;
private _dismantle: () => void;
constructor(private nav: NavController, private navParams: NavParams, private events: Events,
private popoverCtrl: PopoverController) {
this.loc = this.navParams.get('loc');
//set handlers
this._setLoc = (locs) => {this.setLoc(locs);};
this._dismantle = () => {this.dismantle();};
this.events.subscribe(Cons.EVT_LOCS_SAVED, this.setLoc);
this.events.subscribe(Cons.EVT_LOCS_DELETED, this._dismantle);
}
clickMore(ev){
let popover:Popover = this.popoverCtrl.create('LocMorePage',{loc:this.loc});
popover.present({ev: ev});
}
private setLoc(locs: TransLoc[]){
if(!locs || locs.length == 0 || !locs[0]){
return;
}
this.loc = locs[0];
}
private dismantle(){
this.nav.pop();
}
ionViewWillUnload(){
this.events.unsubscribe(Cons.EVT_LOCS_SAVED, this._setLoc);
this.events.unsubscribe(Cons.EVT_LOCS_DELETED, this._dismantle);
}
}
我定义了两个处理程序:_setLoc和_dismantle,用作 事件处理程序分别为事件" Cons.EVT_LOCS_SAVED"," Cons.EVT_LOCS_DELETED"
在测试期间,运行时在语句" this.loc = locs [0];"在函数" setLoc(locs:TransLoc [])"中,值为"这个" as" undefined"
现在非常奇怪的是,我尝试了另一个事件(Cons.EVT_LOCS_DELETED),并且函数" dismantle()"运行顺利,"这个"在声明" this.nav.pop();"正确解析到类" LocationPage"。
的上下文那么究竟是什么导致了不同的结果?
感谢