使用@ angular / router 3.0.0-rc.1向路由添加防护时出错

时间:2016-08-22 19:47:03

标签: angular angular2-routing

当我有以下代码时......

我没有收到错误,我可以干净地导航到pascal模块中的pascal组件

main.ts

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {SuiteModule} from './modules/suite.module';

platformBrowserDynamic().bootstrapModule(SuiteModule);

suite.module.ts

import {NgModule} from '@angular/core';

import {Title} from '@angular/platform-browser';
import {RouterModule} from '@angular/router';

import {AuthenticationModule} from './authentication.module';

import {HomeModule} from './home.module';
import {PascalModule} from './pascal.module';
import {CopernicusModule} from './copernicus.module';

import {SuiteComponent} from './suite.module/suite.component';
import {suiteRouting, suiteRoutingProviders} from './suite.module/suite.routing';

@NgModule({
    imports: [RouterModule, AuthenticationModule, HomeModule, PascalModule, CopernicusModule, suiteRouting],
    providers: [Title, suiteRoutingProviders],
    declarations: [SuiteComponent],
    bootstrap: [SuiteComponent]
})
export class SuiteModule {}

pascal.module.ts

import {NgModule} from '@angular/core';

import {pascalRouting, pascalRoutingProviders} from './pascal.module/pascal.routing';

import {CommonModule} from './common.module';
import {AuthenticationModule} from './authentication.module';

import {PascalComponent} from './pascal.module/pascal.component';

@NgModule({
    imports: [CommonModule, AuthenticationModule, pascalRouting],
    providers: [pascalRoutingProviders],
    declarations: [PascalComponent]
})
export class PascalModule {}

pascal.routing.ts

import {ModuleWithProviders} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';

import {PascalComponent} from './pascal.component';

export const pascalRoutes: Routes = [
    {path: 'pascal', pathMatch: 'full', component: PascalComponent}
];

export const pascalRouting: ModuleWithProviders = RouterModule.forRoot(pascalRoutes);

export const pascalRoutingProviders: any[] = [];

当我添加以下防护等级时:

pascal.guard.ts

import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';

import {AuthenticationService} from '../authentication.module/authentication.service';

import {Observable} from 'rxjs';

export class PascalGuard implements CanActivate {
    constructor(private authenticationService: AuthenticationService) {}

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        return this.authenticationService.isAuthenticated;
    }
}

并尝试将其连接到应用程序中:

pascal.module.ts

import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';

import {CommonModule} from './common.module';
import {AuthenticationModule} from './authentication.module';
import {AuthenticationService} from './authentication.module/authentication.service';

import {pascalRouting, pascalRoutingProviders} from './pascal.module/pascal.routing';

import {PascalComponent} from './pascal.module/pascal.component';

@NgModule({
    imports: [CommonModule, RouterModule, AuthenticationModule, pascalRouting],
    providers: [AuthenticationService, pascalRoutingProviders],
    declarations: [PascalComponent]
})
export class PascalModule {}

pascal.routing.ts

import {ModuleWithProviders} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';

import {PascalComponent} from './pascal.component';
import {PascalGuard} from './pascal.guard';

export const pascalRoutes: Routes = [
    {path: 'pascal', pathMatch: 'full', component: PascalComponent, canActivate: [PascalGuard]}
];

export const pascalRouting: ModuleWithProviders = RouterModule.forRoot(pascalRoutes);

export const pascalRoutingProviders = [PascalGuard];

我收到以下错误:

zone.js:332 Error: (SystemJS) Can't resolve all parameters for PascalGuard: (?).
    Error: Can't resolve all parameters for PascalGuard: (?).
        at new BaseException (http://localhost:8880/vendor/@angular/compiler/src/facade/exceptions.js:27:23)
        at CompileMetadataResolver.getDependenciesMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:551:19)
        at CompileMetadataResolver.getTypeMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:448:26)
        at eval (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:594:41)
        at Array.forEach (native)
        at CompileMetadataResolver.getProvidersMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:575:19)
        at eval (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:582:41)
        at Array.forEach (native)
        at CompileMetadataResolver.getProvidersMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:575:19)
        at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:305:58)
    Evaluating http://localhost:8880/bootstrap.js
    Error loading http://localhost:8880/bootstrap.js

@Injectable添加到防护等级会导致以下错误:

zone.js:332 Error: (SystemJS) Can't resolve all parameters for TypeDecorator: (?).
    Error: Can't resolve all parameters for TypeDecorator: (?).
        at new BaseException (http://localhost:8880/vendor/@angular/compiler/src/facade/exceptions.js:27:23)
        at CompileMetadataResolver.getDependenciesMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:551:19)
        at CompileMetadataResolver.getTypeMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:448:26)
        at eval (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:594:41)
        at Array.forEach (native)
        at CompileMetadataResolver.getProvidersMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:575:19)
        at eval (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:582:41)
        at Array.forEach (native)
        at CompileMetadataResolver.getProvidersMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:575:19)
        at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8880/vendor/@angular/compiler/src/metadata_resolver.js:305:58)
    Evaluating http://localhost:8880/bootstrap.js
    Error loading http://localhost:8880/bootstrap.js

1 个答案:

答案 0 :(得分:2)

如果服务具有构造函数参数,则需要具有@Injectable()装饰器

@Injectable()
export class PascalGuard implements CanActivate {