【问题标题】:How to stop leaving the current page in Ionic 3?如何停止在 Ionic 3 中离开当前页面?
【发布时间】:2017-11-14 03:38:13
【问题描述】:

根据 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;
}

但事实并非如此。出现警报,但页面仍然发生变化。但是这里它说return truereturn false 将告诉Ionic 继续页面更改或停止它。 http://ionicframework.com/docs/api/navigation/NavController/

我做错了什么?

【问题讨论】:

    标签: cordova ionic-framework ionic3


    【解决方案1】:

    您需要使用 Nav Guards。 检查导航卫士部分here

    生命周期钩子是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;
    }
    

    【讨论】:

    • 我也试过了。它什么也没做,甚至不会被触发。可能是因为我使用了一个标签集来导航?
    • 那很奇怪..你能打印cantLeave值吗?
    • 是的,它是一个正确的布尔值。在我的代码中它实际上有点复杂,它从服务中获取它的值,所以我没有在示例中包含它。但它工作正常,这不是问题。
    • 它来自服务的价值..在这种情况下,这里没有足够的信息......也许是因为它的异步?您必须提供minimal reproducible example
    • 不,它不是异步的。这是一个简单的单例服务,它在整个应用程序中存储值。这是一个简单的布尔值。但即使我用单个变量替换它,就像在示例中一样,它也不起作用。
    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多