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

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

    Also, don't fall into the trap of using a literal for the guard class inside your routing configuration, just because some blog articles do:

    { path: 'whatever', component: WhatEverComponent, canActivate: ['WhatEverGuard'] }
    

    is not going to work (No provider for...), instead, use the class directly:

    { path: 'whatever', component: WhatEverComponent, canActivate: [WhatEverGuard] }
    

    Another hint, when lazy loading components, the guard is applied in the routing configuration of the parent component, not in the routing configuration of the lazy loaded component.

提交回复
热议问题