问题描述
我使用的是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中进行了路由,因此该功能确实被调用了两次。我的错,希望这可以对遇到类似问题的人有所帮助