提问人:Matej 提问时间:6/26/2023 最后编辑:Matej 更新时间:6/26/2023 访问量:64
防止使用canDeactivate和中间页面离开应用程序
Prevent leaving app with canDeactivate and intermediate page
问:
在我的 angular 应用程序中,我添加了重定向页面,该页面首先通过路由打开。此页面只有一个目的 - 重定向到主页()。在主页上,我添加了 guard canDeactivate,它正在检查 nextRoute 是否是重定向页面并停止导航。this.router.navigate(['home']);
canDeactivate(
component: HomePage,
currentRoute: ActivatedRouteSnapshot,
currentState: RouterStateSnapshot,
nextState: RouterStateSnapshot
): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
console.log('canDeactivate', currentRoute, currentState, nextState);
if(!nextState.url.includes('redirect')) {
return true;
} else {
const currentUrl = currentState.url;
if (this.location.isCurrentPathEqualTo(currentUrl)) {
this.router.navigate([currentUrl], { skipLocationChange: true });
} else {
history.pushState(null, null, location.href);
}
return false;
}
}
此代码仅在 firefox 中正常工作,但在 chrome 或 safari 中不起作用。在后退按钮上,用户不会被重定向到重定向页面,而是重定向到之前的页面,并且不会触发 canDeactivate。在 chrome 或 safari 中,它仅在用户与页面交互(通过单击等)时才有效。我认为存在一些问题,即重定向页面在用户与之交互之前不会推送到历史记录。有没有其他方法可以实现这一点?
我需要此功能,因为我的页面具有外部身份验证,并且需要阻止用户返回登录页面。
答: 暂无答案
评论