Ionic 2 - Disabling back button for a specific view

前端 未结 5 2045
小鲜肉
小鲜肉 2020-12-04 15:26

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.

What I\'m doing is this.nav.

5条回答
  •  -上瘾入骨i
    2020-12-04 16:20

    Ionic2 hides the menu button, if you are not on the root page and shows the back button.

    As you said, the animation is missing with:

    this.view.setRoot(SomePage);
    

    Write this for an animation with "back" or "forward":

    this.nav.setRoot(SomePage, {}, {animate: true, direction: "forward"});
    

    Okay, what if I need the default animation which is provided and is not "forward" or "back"?

    There are some ways:

    1. This will provide the default animation

    In your current Page, write:

    this.nav.insert(0, SomePage).then(() => {
        this.nav.popToRoot();
    });
    

    2. Don't set it as root for whatever reason

    this.view.push(SomePage);
    

    Okay fine, now we need to take care of a view things.

    1. part: Hide the back button
    2. part: Because the page is not the root anymore we need to show the normal menu icon again (otherwise there wouldn't be any icon at all after just hiding the back button).

    Notice the menuIsHidden property.

    export class SomePage {
        // Part 2:
        menuIsHidden: boolean = false;
    
        constructor(private nav: NavController, private view: ViewController) {}
    
        // ionic2 will call this automatically
        ionViewWillEnter() {
            // Part 1:
            this.view.showBackButton(false);
        }
    }
    

    somePage.html

    
     
        
        
      
    
    

    I hope this will help someone.

提交回复
热议问题