How to cancel route change in Angular 2?

后端 未结 1 421
灰色年华
灰色年华 2020-12-19 06:17

I would like to cancel the route change when the user click back or forward button in the browser. So far I manage to capture the route change event as in the code below:

相关标签:
1条回答
  • 2020-12-19 06:41

    You may use canDeactivate guard when defining route, this will help you writing logic when changing route.

    You may simply return true or false, based upon if you want to continue or reject route navigation, you also get reference to your component instance which is active for that route so as to read any property on it.

    Signature of the method is like below,

    canDeactivate(component: T,
        route: ActivatedRouteSnapshot, 
        state: RouterStateSnapshot) :  
         Observable<boolean>|Promise<boolean>|boolean
    

    You may read more about it here

    0 讨论(0)
提交回复
热议问题