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 属性。
这是因为你的
C
中的canActivate
中有一个大写的APP-ROUTES
字母。
确保您已在 AuthGuardService
提供商数组中注册了 app.module.ts
。
// route
const APP_ROUTES = [{
path: 'login',
CanActivate: [AuthGuardService], // fix here
component: login
}