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

后端 未结 12 2074
陌清茗
陌清茗 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 ] 
      },
    ];
    
    0 讨论(0)
  • 2021-02-03 20:42
    import { Injectable } from '@angular/core';
    import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    
    @Injectable()
    export class AuthGuard implements CanActivate {
    
        constructor(private router: Router) { }
    
        canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
            if (localStorage.getItem('currentUser')) {
                // logged in so return true
                return true;
            }
    
            // not logged in so redirect to login page with the return url
            this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
            return false;
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
      ],
      ...
    
    0 讨论(0)
  • 2021-02-03 20:52

    Actually, it was only a typo in an import...

    I was typing

    import { AuthGuard } from './../Authentification/auth.guard';

    instead of

    import { AuthGuard } from './../authentification/auth.guard';

    making it not working but at the same time not displaying me any error...

    (sadface)

    0 讨论(0)
  • 2021-02-03 20:52

    Importing both HttpModule and HttpClientModule helped me.

    import { HttpClientModule } from '@angular/common/http'; 
    import { HttpModule } from '@angular/http';
    
    0 讨论(0)
提交回复
热议问题