Miguel Frias
Miguel Frias

Reputation: 2710

Angular auth guard Can't resolve all parameters

im tryin to create an auth.guard.service but for some reason i get error Can't resolve all parameters for AuthGuard: (?, [object Object])., here is the services.

import { Observable } from 'rxjs/Observable';
// import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  user = true;
  constructor(private auth: any, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | boolean {
    if (this.user) {
      return true;
    }
    this.router.navigate(['']);
    return false;
  }
}

and here is the app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavComponent } from './components/nav/nav.component';
import { HomeComponent } from './components/home/home.component';
import { GalleryComponent } from './components/gallery/gallery.component';
import { FooterComponent } from './components/footer/footer.component';

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ScrollToModule } from 'ng2-scroll-to';
import { NgxCarouselModule } from 'ngx-carousel';
import { AnimateOnScrollModule } from 'ng2-animate-on-scroll';

import { AuthGuard } from './services/auth.guard.service';

import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';


const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'gallery', component: GalleryComponent, canActivate: [AuthGuard] }
];

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    NavComponent,
    HomeComponent,
    GalleryComponent,
    FooterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgxCarouselModule,
    NgbModule.forRoot(),
    ScrollToModule.forRoot(),
    RouterModule.forRoot(routes),
    AnimateOnScrollModule.forRoot(),
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  exports: [
    TranslateModule
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }

i dont know where could be error is, the services that im importin on the auth.guard.services is commented because i not using it yet, just in case, for some reason i can resolve all the parameters if somebody have an idea.

Upvotes: 0

Views: 1242

Answers (2)

bygrace
bygrace

Reputation: 5988

The error you are getting is a run-time error related to Angular Dependency Injection. It can't determine how to create the dependency represented by the first constructor parameter because its type is any. You can change the type from any to some type that is provided or use @Inject on a type or token that is provided. Angular DI shouldn't have a problem with the second parameter (Router).

Upvotes: 1

Samuel Shyu
Samuel Shyu

Reputation: 151

You can use "any" in your constructor. constructor(private auth: any, private router: Router). Change it to your service like authService.

Upvotes: 0

Related Questions