![](/img/trans.png)
[英]Why is the Angular canActivate function guard executed twice when navigating routes
[英]Angular canActivate function in guard executed twice
我正在使用 Angular 8,如果滿足特定條件,我會使用 window.open(url) 在單獨的選項卡中打開一個頁面,然后返回 false 因為我不想離開當前頁面我是在。
我在 canActivate 函數中執行此邏輯:
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean | UrlTree> |
Promise<boolean | UrlTree> | boolean | UrlTree {
if(condition)
window.open(url)
return false;
}
我的問題是 canActivate 函數被連續調用兩次,這會導致新選項卡打開兩次。 我該如何解決這個問題?
這就是我在 app.routing.constant 中路由到我的守衛的方式
{
path: dashboardRoutes.MY_ROUTE,
component: BlankComponent,
canActivate: [MyGuard]
},
發現問題,我使用[routerLink]
在 html 組件中路由,並使用this.router.navigate
在 ts 中this.router.navigate
,所以這個 func 確實被調用了兩次。 我的錯誤,希望這可以幫助任何有類似問題的人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.