相关疑难解决方法(0)

Angular 2 CanActivate被调用两次

您好我遇到了问题.当我尝试导航到不被允许的页面时,我的CanActivate警卫会被调用两次,因为我没有登录.

我有1个根模块,并提供了我的CanActivate后卫和其他服务.

先感谢您!

这是我的路由器:

const appRoutes: Routes = [
    {
        path: "",            
        pathMatch: "full",
        redirectTo: "/meal-list",
    },
    {
        path: "login",
        component: LoginComponent,
    },
    {
        path: "meal-list",
        component: MealListComponent,
        canActivate: [AuthActivateGuard],
    }  
];


export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, {useHash: true});
Run Code Online (Sandbox Code Playgroud)

守卫:

@Injectable()
export class AuthActivateGuard implements CanActivate {


  constructor(private authService: AuthService,
              private router: Router) {
    console.log("guard created");
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|boolean {
    if (!this.authService.authenticated) {
      return this.authService.checkLogged().map(res => {
        this.authService.authenticated = true;
        return true;
      }).catch(()=> {
        this.authService.authenticated = …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

10
推荐指数
2
解决办法
2979
查看次数

标签 统计

angular ×1

angular2-routing ×1