CanActivate 路由防护未被调用

问题描述 投票:0回答:1
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
import { Observable } from 'rxjs/Observable';
    
@Injectable()
export class AuthGuardService implements CanActivate {
    
  constructor() { }
        
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
    console.log("can activate called");
    return true;
  }
}

// route
const APP_ROUTES = [{
  path: 'login',
  CanActivate: [AuthGuardService],
  component: login
}

因此,当登录页面加载时,它不会调用可以激活方法和打印控制台。我创建了一个守卫服务并在路线中添加了 CanActivate 属性。

angular angular-router angular-router-guards
1个回答
1
投票

这是因为你的

C
中的
canActivate
中有一个大写的
APP-ROUTES
字母。 确保您已在
AuthGuardService
提供商数组中注册了
app.module.ts

// route
const APP_ROUTES = [{
  path: 'login',
  CanActivate: [AuthGuardService], // fix here
  component: login
}
© www.soinside.com 2019 - 2024. All rights reserved.