//看守服务
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 属性。