admin管理员组文章数量:1325589
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
}
});
}
本文标签: javascriptAngular 5 prevent or warn user before back button clickStack Overflow
版权声明:本文标题:javascript - Angular 5 prevent or warn user before back button click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742189988a2430032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论