I'm developing a web site using Angular 2. Is there any way to disable or trigger Browser back button using Angular 2?
Thanks
I'm developing a web site using Angular 2. Is there any way to disable or trigger Browser back button using Angular 2?
Thanks
Not sure if this is already sorted, but posting the answer nonetheless, for future references. To tackle this, you basically need to add a listener in your app-component and setup a canDeactivate guard on your angular-router.
step 1: Import Locatoion from angular commmon
step 2: Initialise in constructor
step 3: Add function in ngOnInit of the respective coponent,
});
Note: Here /basic-info will be replaced by your path.
If first time it is not working, try adding outside subscribe,
This issue occurs on IE browser. Use below mentioned code it will resolve your issue.
If you want to prevent a route to be reached you can add the @CanActivate() decorator to your routing component
See also
- Angular 2: Inject a dependency into @CanActivate? for access to global services.
- Angular2 Router - Anyone know how to use canActivate in app.ts so that I can redirect to home page if not logged in
A bit late perhaps but maybe somebody can use it. This is a solution I use for a page with tabs (Bootstrap 4 style) where each tab is a component.
NavigationGuard like @Jithin Nair above but also broadcasts when an attempt to navigate was made and whether it was permitted. Subscribers of CanNavigateService can use it to decide what to do instead of back navigation.
Usage:
This isn't Angular2 related problem. You can send the user back in history. See Manipulating the browser history,
history.go()
method particular:However, I don't think there's a way to cancel or disable default browser action on pressing back button in the browser window because that could be very easily abused.
As an alternative you can show a dialog window when user tries to leave the page: javascript before leaving the page