admin管理员组文章数量:1355655
I have a ponent that should be destroyed each time the changes (for example, each time the user click to go back on history), but before the destruction of the ponent, I want to show a message asking if the user wants to leave without saving the changes.
The user can select the "no" answer, which the app should stop the navigation in this case. If the user selects the "yes" answer, the navigation should continue.
My question is, how can I do that?
Follows an example:
export class ListConfigurationsComponent implements OnInit, OnDestroy {
...
constructor(
...
private router: Router) {
router.events.subscribe((val) => {
//Cancel the option here
});
}
}
I have a ponent that should be destroyed each time the changes (for example, each time the user click to go back on history), but before the destruction of the ponent, I want to show a message asking if the user wants to leave without saving the changes.
The user can select the "no" answer, which the app should stop the navigation in this case. If the user selects the "yes" answer, the navigation should continue.
My question is, how can I do that?
Follows an example:
export class ListConfigurationsComponent implements OnInit, OnDestroy {
...
constructor(
...
private router: Router) {
router.events.subscribe((val) => {
//Cancel the option here
});
}
}
Share
Improve this question
edited Sep 26, 2019 at 13:52
Ricardo Rocha
asked Sep 26, 2019 at 12:05
Ricardo RochaRicardo Rocha
16.3k23 gold badges84 silver badges140 bronze badges
2
-
2
I think you are looking for
CanDeactivate
guard - angular.io/api/router/CanDeactivate – user2216584 Commented Sep 26, 2019 at 12:06 - @user2216584 I believe it is not what he is looking for, since route guard is for an entire ponent. – Kiril1512 Commented Sep 26, 2019 at 12:18
4 Answers
Reset to default 4try this using restoredState property, it's defined when the navigation is triggered by a popstate event otherwise it's null, check this
router.events.pipe(
filter(( event: NavigationEvent ) => {
return( event instanceof NavigationStart );
})
).subscribe((event: NavigationStart) => {
if (event.restoredState ) {
// display you confirmation modal
}
})
You're looking for CanDeactivate Guard. Check this article, it describes how to make generic CanDeactivate strategy: https://medium./@tobias.ljungstrom/how-to-use-a-custom-dialogue-with-the-candeactivate-route-guard-in-angular-385616470b6a
One version could be that you don't use the [routerLink] in your template. Use the (click) handler instead to call your function which checks if the user should be redirected.
In your html
(click) = "checkDialog()";
If so in your ts just use
this.router.navigate(['/next-page']);
in your condition
A better approach would be to check for the navigationTrigger
property on the NavigationEvent
and check the previous state ID in the restoredState
property as
if(event instanceof NavigationStart && event.navigationTrigger == "popstate" && event.restoredState)
Stackblitz at https://stackblitz./edit/angular-check-navigation-back-fron-through-browser
本文标签: javascriptAngular Router (Routing)Cancel navigation to different route (url)Stack Overflow
版权声明:本文标题:javascript - Angular Router (Routing) - Cancel navigation to different route (url) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744052643a2582703.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论