2017-04-19 62 views
-1

我正在使用ionic2。 我有幾頁和流 - P1 - > P2 - > P3。防止返回頁面按鈕,Ionic2在特定條件下

我想在P3上使用ionViewCanLeave(),這樣如果某些條件不符合,它應該在用戶按下後退按鈕時發出警報。

this.navCtrl.pop() 
    .catch(() => { 
    console.log('should I stay or should I go now'); 
    }); 

ionViewCanLeave(): boolean{ 
    // here we can either return true or false 
    // depending on if we want to leave this view 
    let percentMarks = Math.floor((this.totalCorrectAnswers/this.questionSetChosen.length) * 100); 
    if(percentMarks > 0){ 
     return true; 
    } else { 
     return false; 
    } 
} 

但它給了一個錯誤。

+0

它給了什麼錯誤? –

回答

0

我認爲你做錯了...這個功能ionViewCanLeave運行休假所以在那一刻你應該檢查是否可能。希望這可以幫到你。

ionViewCanLeave() { 
    let percentMarks = Math.floor((this.totalCorrectAnswers/
        this.questionSetChosen.length) * 100); 

    if(percentMarks > 0){ 
    this.navCtrl.pop(); 
    } else { 
    console.log('i should stay')); 
    }