0

我正在使用 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]
},
4

1 回答 1

0

发现了问题,我在 html 组件中使用 using[routerLink]和在 ts 中使用using 进行路由this.router.navigate,所以这个 func 真的被调用了两次。我的错误,希望这可以帮助任何有类似问题的人

于 2020-09-08T07:54:30.277 回答