实现CanActivate Guard错误时:没有HRComponent的提供者

时间:2017-08-20 06:16:23

标签: angular angular2-forms canactivate

实现CanActivate Guard Im时出现以下错误:没有HRComponent的提供者

import { Component, OnInit } from "@angular/core"
import { ActivatedRoute, CanActivate } from "@angular/router"
import { LoginService } from "../../services/loginservice"
@Component({
    templateUrl:"../../../templates/hrmodule/hrmodule.html"
})
export class HRComponent implements CanActivate {
    constructor(private Loginservice: LoginService) { }
    canActivate(){
        debugger;
        alert('Active');
        return true;
    }
}

这是我的HRmodile.ts

import { NgModule } from "@angular/core"
import { RouterModule} from "@angular/router"
import { HrRoute } from "../../app/route/hrroute"
import { HRComponent } from "../../app/component/hrcomponent/hrcomponent"
import { LoginService } from "../../app/services/loginservice"
@NgModule({
    imports: [RouterModule.forChild(HrRoute)],
    declarations: [HRComponent],
    bootstrap: [HRComponent ],
    providers: [LoginService]
})

export class HRmodule {
}

这是HrRoute在这里我写了canActivate

import { HRComponent } from "../component/hrcomponent/hrcomponent"
export const HrRoute = [
    { path: "Add", component: HRComponent, canActivate: [HRComponent]}
];

这是我的Html链接

    <a [routerLink]="['HrModule/Add']" class="btn btn-primary btn-sm">HrModule</a>

{ path: 'HrModule', loadChildren: '../../module/hrmodule/hrmodule#HRmodule' },

1 个答案:

答案 0 :(得分:1)

警卫必须是服务而不是组件

|awk '{print $2, $3}'

并注意它不能成为模块中的自举组件。按照惯例,引导组件是“AppComponent”

@Injectable()
export class HRService implements CanActivate {
    constructor(private Loginservice: LoginService) { }
    canActivate(){
        debugger;
        alert('Active');
        return true;
    }
}

请参阅此处的文档以获取更多信息和示例:https://angular.io/api/router/CanActivate

在这里:https://angular.io/guide/router#milestone-5-route-guards