“No provider for AuthGuard!” using CanActivate in Angular 2

后端 未结 12 2080
陌清茗
陌清茗 2021-02-03 20:09

EDIT : Obviously this is outdated, now you provide your guard at the providers array in an NgModule. Watch other answers or official documentation

12条回答
  •  一整个雨季
    2021-02-03 20:49

    The answer is further down in the tutorial. See the file listings in the "Add the LoginComponent" topic under the "Component-less route:..." section in "Milestone 5: Route Guards". It shows AuthGuard and AuthService being imported and added to the providers array in login-routing.module.ts, and then that module being imported into app.module.ts.

    login-routing.module.ts

      ...
        import { AuthGuard }            from './auth-guard.service';
        import { AuthService }          from './auth.service';
        ...
        @NgModule({
        ...
          providers: [
            AuthGuard,
            AuthService
          ]
        })
        export class LoginRoutingModule {}
    

    app.module.ts

    import { LoginRoutingModule }      from './login-routing.module';
    
    @NgModule({
      imports: [
        ...
        LoginRoutingModule,
        ...    
      ],
      ...
      providers: [
        DialogService
      ],
      ...
    

提交回复
热议问题