2017-04-19 64 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')); 
    }