Ionic 4 Angular router navigate and clear stack/history of previous page

后端 未结 2 1264
暖寄归人
暖寄归人 2021-01-06 14:04

I\'m developing an app using Ionic 4 with Angular router. I would like to navigate to another page and clear the page stack. In Android native, it\'s something like this:

2条回答
  •  生来不讨喜
    2021-01-06 14:40

    For example of a login page, which is a root page ('/login'), after clicking the login button navigate to a new page like this:

    onLogin() {
       this.router.navigateByUrl('/profile', { replaceUrl: true }) 
    }
    

    It replaces the page history entry with a new URL, in this particular case with '/profile'. After using a plain or the browser's back button, you will not be redirected to the login page but to the preceding page. Let's say your page navigation is like this: home -> login -> profile, but the history will remember only home -> profile, thus hitting the back button at profile page will take you back to home.

    For a complete solution, you can combine this with Angular Route Guards, to prevent accessing pages based on some conditions (e.g. user is logged in). Hints how to do it can be found in this answer.

提交回复
热议问题