2017-05-26 94 views
1

工作,我注意到,每一次我在推棧頁面時,它調用ionViewloaded和我添加subscribe()我的問題是我不能退訂,當我彈出的頁面。這是我的代碼。它不起作用。訂閱和退訂不離子2

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)); 
} 

回答

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(); 
} 
+0

並訂閱來自rxjs /接收? 我收到了一個警告類型void無法指定鍵入'Subscription' – domzgarcia

+0

'import {subscription} from「rxjs/Subscription」;' –

+0

this.events.subscribe()return void無法分配給訂閱 – domzgarcia