首页 > 解决方案 > Angular 8 canActivate on refresh

问题描述

我试图canActivate在 angular 8 上使用(过去 2 年我一直在使用),但我似乎无法让它工作。

在刷新/f5 上,我什至看不到网络上的请求,当它进入响应时,它给了我“无法读取未定义的属性长度”

这是代码:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
  const user = this.cookieSerivce.getUserCookie();
  if (!this.userService.getUserIfExists().Authorization && user) {
    this.userService.setUser(user);
  }

  return this.usersEndpointService.validateUserToken().pipe(
    map((res) => {
      if (!res.code) return true;

      this.wrongToken();
      return false;
    },
  }));
}

const routes: Routes = [
  {path: '', component: LoginComponent},
  {path: 'forgot-password', component: ForgotPasswordComponent},
  {path: 'reset-password', component: ResetPasswordComponent},
  {path: 'dashboard', component: DashboardComponent, canActivate: [ValidTokenService]},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我错过了什么?我确定它甚至没有发送请求,在网络上看不到它,也没有在后端得到它,为什么?

标签: angulartypescriptangular8

解决方案


推荐阅读