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

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

    This happened to me when I had setup my Routes incorrectly:

    WRONG

    const routes: Routes = 
    [
      { 
        path: 'my-path', 
        component: MyComponent, 
        resolve: { myList: MyListResolver, canActivate: [ AuthenticationGuard ] } 
      },
    ];
    

    Note that in this case canActivate was accidentally made a part of the resolve object.

    CORRECT

    const routes: Routes = 
    [
      { 
         path: 'my-path', 
         component: MyComponent, 
         resolve: { myList: MyListResolver }, 
         canActivate: [ AuthenticationGuard ] 
      },
    ];
    

提交回复
热议问题