我有一个 Angular 2.0.1(最终版)应用,它使用 HashLocationStrategy 作为路线导航策略。
我定义了一条路线如下:
{
path: 'shiftmanage', component: ShiftManageComponent,
canLoad: [AuthGuard],
canActivate: [AuthGuard]
},
这是 AuthGuard 类:
import { Injectable } from '@angular/core';
import {
Route,
Router,
CanLoad,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanLoad, CanActivate {
constructor(private router: Router) {
console.log("AuthGuard constructor")
}
canLoad(route: Route): boolean {
if (route.path === "shifts") {
return true;
} else {
return false;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (route.routeConfig.path === "shiftmanage") {
return true;
} else {
return false;
}
}
}
然后我将这个保护类添加到 NgModule Provider 中,如下所示:
providers: [
AuthGuard,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
... other providers
]
导航正常,每当我尝试导航到 shiftmanage 路径时,canActivate 路由守卫就会被触发。
问题 canLoad 路由守卫从未被击中。
问题:
这个 canLoad 守卫是否因为 HashLocationStrategy 而没有被击中,还是我做错了什么?
最佳答案
canLoad
用于加载lazy-loaded modules使用 loadChildren
{
path: 'child',
canLoad: [AuthGuard],
loadChildren: 'path/to/child.module'
}
关于angular - 为什么路由守卫 canLoad 不开火,但 canActivate 开火,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011143/