So I have a SPA application built in angular, now my issue is when a user is on my app if they press the (browser) back button, sometimes if data was sent on the previous page it can cause errors and sometime there is state that when refreshed goes away. Now Is there a way I can warn a user before going back or simply not allow a user to go back??
I have tried to do this in my index.html
<script>
window.onbeforeunload = function() {
return "Message";
};
</script>
but it looks like this no longer works on newer browsers, I found a similar question here but its from a few years ago and I've tried a few of there solutions and none of them worked..
what is the best way in 2018 to handle this situation??
Thanks
So I have a SPA application built in angular, now my issue is when a user is on my app if they press the (browser) back button, sometimes if data was sent on the previous page it can cause errors and sometime there is state that when refreshed goes away. Now Is there a way I can warn a user before going back or simply not allow a user to go back??
I have tried to do this in my index.html
<script>
window.onbeforeunload = function() {
return "Message";
};
</script>
but it looks like this no longer works on newer browsers, I found a similar question here but its from a few years ago and I've tried a few of there solutions and none of them worked..
what is the best way in 2018 to handle this situation??
Thanks
Share Improve this question edited Sep 10, 2018 at 0:48 Smokey Dawson asked Sep 9, 2018 at 23:47 Smokey DawsonSmokey Dawson 9,24022 gold badges85 silver badges162 bronze badges 1-
Take a look at
canDeactivate()
– Ikhlak S. Commented Sep 10, 2018 at 1:09
2 Answers
Reset to default 5You can allow/prevent navigating to a route with a canActivate
route guard, and you can allow/prevent navigating away from a route with a canDeactivate
route guard (see the Angular documentation).
If you want to prevent a user from going back to a page that has already been visited, use a canActivate
route guard. In this stackblitz, a confirmation is asked before going back to the Login
page. As a bonus, it also contains an example of a canDeactivate
route guard for the Home
route.
Here is the code for the canActivate
route guard of the Login
route:
activate-guard.ts
import { Injectable } from "@angular/core";
import { CanActivate, CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
import { Observable } from "rxjs";
import { LoginActivateService } from "./login-activate.service";
@Injectable()
export class ActivateGuard implements CanActivate {
constructor(private loginActivate: LoginActivateService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.loginActivate.canActivateLogin || confirm("Do you really want to go back to login?");
}
}
app-routing.module.ts
...
imports: [
RouterModule.forRoot([
{
path: 'login', ponent: LoginViewComponent,
canActivate: [ActivateGuard]
},
{
path: 'home',
ponent: HomeViewComponent,
canDeactivate: [DeactivateGuard]
},
...
])
],
you can subscribe to the events property of the Router (available in Angular 8 >) it will trigger an action on each routing phase for example at navigation start, end , onPopstate event, ... and thus the browser back button event.
import { Router } from '@angular/router';
constructor(private router: Router) {
this.router.events.subscribe((event) => {
if (event instanceof NavigationStart && event.navigationTrigger === 'popstate') {
// write the logic here
}
});
}