Angular 为什么路线守卫不能开火,但可以激活
我有一个angular 2.0.1(最终版)应用程序,它使用HashLocationStrategy作为路线导航策略 我将其中一条路线定义如下:Angular 为什么路线守卫不能开火,但可以激活,angular,typescript,angular2-routing,Angular,Typescript,Angular2 Routing,我有一个angular 2.0.1(最终版)应用程序,它使用HashLocationStrategy作为路线导航策略 我将其中一条路线定义如下: { path: 'shiftmanage', component: ShiftManageComponent, canLoad: [AuthGuard], canActivate: [AuthGuard] }, 以下是AuthGuard类: import { Injectable }
{
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路由保护就会被点击
问题:无法加载路线守卫
问题:
这是因为HashLocationStrategy没有命中这个canLoad防护还是我做错了什么?用于加载loadChildren
{
path: 'child',
canLoad: [AuthGuard],
loadChildren: 'path/to/child.module'
}
好的,在仔细阅读链接文档之后,这很有意义,谢谢