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

后端 未结 12 2078
陌清茗
陌清茗 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:45

    For those who still have this error - don't forget to include your AuthGuard service or class to main bootstrap function. And don't forget to import this service before bootstrap runs.

    import { bootstrap } from '@angular/platform-browser-dynamic';
    
    import { AppComponent } from './app.component';
    import { AuthGuard } from './shared/auth.service';
    
    bootstrap(AppComponent, [
      appRouterProviders,
      AuthGuard
    ]);
    

    Angular 2 team did not mention this in main router docs, and it took couple of hours for me to figure it out.

提交回复
热议问题