Ionic 2 - 禁用特定视图的后退按钮 [英] Ionic 2 - Disabling back button for a specific view

查看:116
本文介绍了Ionic 2 - 禁用特定视图的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Ionic 2 上搞砸了一下,我想知道如何禁用特定视图的后退按钮。

So I'm messing around a bit with Ionic 2, and I want to know how to disable the back button for a specific view.

我正在做的是 this.nav.push(SomePage); 它可以工作,但 NavController 自动为我提供一个后退按钮。如何禁用后退按钮?

What I'm doing is this.nav.push(SomePage); It works, but the NavController automatically puts a back button there for me. How do I disable the back button?

注意:我知道我可以使用 this.nav.setRoot(SomePage)将SomePage设置为root并且没有后退按钮,但是这不会提供 NavController 自动执行的有用动画。

NOTE: I do know that I can use this.nav.setRoot(SomePage) to set the SomePage as root and not have a back button, but that doesn't provide the useful animation that NavController automatically does.

编辑:这个问题有些陈旧,但它引起了一些关注,所以我认为提及将来的另一个也是合适的之所以你可能不想使用 this.nav.setRoot 来推送没有后退按钮的页面:它会删除预先存在的页面堆栈。因此,如果您仍希望能够在不向用户提供UI方式的情况下返回代码中的上一页, setRoot 将不允许您这样做。

This question is somewhat old, but it has garnered some attention, so I think it would also be appropriate to mention for future reference that there's another reason you may not want to use this.nav.setRoot in order to push a page with no back button: it erases the preexisting stack of pages. So if you wanted to still be able to go back to the previous page in code without giving the user a UI way to do so, setRoot wouldn't allow you to do that.

推荐答案

选项1

通过将 hideBackButton 属性添加到 ion-navbar 组件

<ion-navbar hideBackButton="true">
    <ion-title>Sub Page</ion-title>
</ion-navbar>

选项2

使用 ViewController 类提供的 .showBackButton(bool)方法从页面类中隐藏它

Hide it from within the page class by using the .showBackButton(bool) method provided by the ViewController class

import { NavController, ViewController } from 'ionic-angular';

export class SubPage {

    constructor(public navCtrl: NavController, private viewCtrl: ViewController) { }

    ionViewWillEnter() {
        this.viewCtrl.showBackButton(false);
    }

}

来自 Ionic docs


请务必在ionViewWillEnter之后调用此方法以确保已呈现DOM

Be sure to call this after ionViewWillEnter to make sure the DOM has been rendered.

注意

我想补充一点,这些选项在硬件时不予考虑按下后退按钮。硬件后退按钮仍可能导致活动页面从导航堆栈中弹出。

I'd just like to add that these options don't take into account when the hardware back button is pressed. The hardware back button is still likely to cause the active page to pop from the nav stack.

这篇关于Ionic 2 - 禁用特定视图的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆