路线警卫为什么不能开火,但是阿拉查瓦特能做到

J K*_*ing 7 typescript angular2-routing angular

我有一个角度2.0.1(最终)应用程序,它使用HashLocationStrategy作为路径导航策略.

我定义了一条路线,如下所示:

    { 
    path: 'shiftmanage', component: ShiftManageComponent,
    canLoad: [AuthGuard],
    canActivate: [AuthGuard] 
    },
Run Code Online (Sandbox Code Playgroud)

这是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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我将这个防护类添加到NgModule提供程序中,如下所示:

providers: [
    AuthGuard,
    { provide: LocationStrategy, useClass: HashLocationStrategy }
    ... other providers
]
Run Code Online (Sandbox Code Playgroud)

每当我尝试导航到班次管理路径时,导航工作和canActivate路线防护就会被击中.

问题:canLoad路线后卫是从不打.

题:

这个canLoad后卫是不是因为HashLocationStrategy被击中了还是还有别的我做错了?

Pau*_*tha 20

canLoad用于加载延迟加载模块loadChildren

{
  path: 'child',
  canLoad: [AuthGuard],
  loadChildren: 'path/to/child.module'
}
Run Code Online (Sandbox Code Playgroud)