我有一个角度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提供程序中,如下所示:
providers: [
AuthGuard,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
... other providers
]
每当我尝试导航到 shiftmanage 路径时,导航工作和 canActivate 路线防护都会被命中 .
PROBLEM: canLoad 路线守卫永远不会被击中 .
问题:
这个 canLoad 后卫是不是因为HashLocationStrategy被击中了还是还有别的我做错了?
1 回答
canLoad用于使用
loadChildren
加载lazy-loaded modules