Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 为什么路线守卫不能开火,但可以激活_Angular_Typescript_Angular2 Routing - Fatal编程技术网

Angular 为什么路线守卫不能开火,但可以激活

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 }

我有一个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提供程序中,如下所示:

providers: [
    AuthGuard,
    { provide: LocationStrategy, useClass: HashLocationStrategy }
    ... other providers
]
每当我尝试导航到shiftmanage路径时,导航会起作用,canActivate路由保护就会被点击

问题:无法加载路线守卫

问题: 这是因为HashLocationStrategy没有命中这个canLoad防护还是我做错了什么?

用于加载
loadChildren

{
  path: 'child',
  canLoad: [AuthGuard],
  loadChildren: 'path/to/child.module'
}

好的,在仔细阅读链接文档之后,这很有意义,谢谢