订阅和取消订阅不在Ionic 2中工作

时间:2017-05-26 07:49:30

标签: angular typescript ionic2

我注意到每次我在堆栈中推送页面时都会调用ionViewloaded并添加subscribe()我的问题是当我弹出页面时我无法取消订阅。这是我的代码。它不起作用。

ionViewDidLoad() {
    this.user = this.navParams.data;
    this.events.subscribe('close:userPopOver', this.onRemovePopOver.bind(this));
}

ionViewWillLeave() {
    alert('FIRES!!');
    this.events.unsubscribe('close:userPopOver', this.onRemovePopOver.bind(this));
}

2 个答案:

答案 0 :(得分:2)

两个答案都无法实现,因为打字稿没有识别传递给订阅实例的void值,使其工作 this.events.unsubscribe('close:userPopOver',null)

https://forum.ionicframework.com/t/events-pub-sub-how-to-unsubscribe-in-lifecycle-hook/50596/5

答案 1 :(得分:0)

private sub : Subscription;

ionViewDidLoad() {
    this.user = this.navParams.data;
    this.sub = this.events.subscribe('close:userPopOver', this.onRemovePopOver.bind(this) );
 }

ionViewWillLeave(){
    alert('FIRES!!');
    this.sub.unsubscribe();
 }