如何停止离开 Ionic 3 中的当前页面?

Tam*_*gár 1 cordova ionic-framework ionic3

根据 Ionic 3 文档,这应该可以工作:

ionViewWillLeave(): boolean {
    if (this.cantLeave) {
        let alert = this.alertCtrl.create({
            title: 'You can\'t leave',
            subTitle: 'You stay and work.',
            buttons: ['Oh sorry']
        });
        alert.present();
    }
    return this.cantLeave;
}
Run Code Online (Sandbox Code Playgroud)

然而它没有。警报出现,但页面仍会发生变化。但是这里它说return truereturn false会告诉 Ionic 继续页面更改或停止它。 http://ionicframework.com/docs/api/navigation/NavController/

我究竟做错了什么?

Sur*_*Rao 6

您需要使用Nav Guards。在此处查看导航卫士部分。

生命周期钩子ionViewCanLeave()不是ionViewWillLeave

ionViewCanLeave(): boolean {
    if (this.cantLeave) {
        let alert = this.alertCtrl.create({
            title: 'You can\'t leave',
            subTitle: 'You stay and work.',
            buttons: ['Oh sorry']
        });
        alert.present();
    }
    return this.cantLeave;
}
Run Code Online (Sandbox Code Playgroud)