12

I have a code for AuthGuard.service.ts as follows.

import { Injectable } from '@angular/core';
import { ActivatedRoute, CanActivate } from '@angular/router';
import { userLoginService } from './userLoginService';
export class AuthGuard implements CanActivate {
    constructor(private service: userLoginService) { }
    canActivate() {
        if (this.service.IsloggedIn()) {
            return true;
        } else {
            window.alert(' You are not logged in.Please LogIn ');
            return false;
        }
    }

}

In userLoginService.ts, I have code as follows

export class userLoginService {
    constructor() {}
    IsloggedIn(): boolean {
        return false;
    }
}

I am injecting this AuthGuard.service.ts in my route as follows. And I have also provided this service name in providers of NgModule.

    const appRoutes: Routes = [
      { path: '', component: HomePageComponent, pathMatch: 'full' },
      { path: 'CartItems', component: CartItemsComponent, pathMatch: 'full', canActivate: [ AuthGuard ]},
    ];
@NgModule({
.
.
.
 providers: [UserInfoService , AuthGuard],
.
.
.

Now when executing this code I am getting an error as follows.

Uncaught Error: Can't resolve all parameters for AuthGuard: (?).
    at syntaxError (compiler.js:466)
    at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15547)
    at CompileMetadataResolver._getTypeMetadata (compiler.js:15382)
    at CompileMetadataResolver._getInjectableMetadata (compiler.js:15362)
    at CompileMetadataResolver.getProviderMetadata (compiler.js:15722)
    at eval (compiler.js:15633)
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver._getProvidersMetadata (compiler.js:15593)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15161)
    at JitCompiler._loadModules (compiler.js:33542)

Would you please let me know where I have committed a mistake.

roopteja
  • 721
  • 2
  • 16
  • 36

3 Answers3

32

I see you import Injectable but you never use it !!

In your AuthGuard.service.ts. you should add : @Injectable() above your class as like :

@Injectable()
export class AuthGuard implements CanActivate {
    ...
}
Community
  • 1
  • 1
Paris Benamour
  • 327
  • 2
  • 3
0

Uncaught Error: Can't resolve all parameters for AuthGuard: (?). at syntaxError (compiler.js:431) at CompileMetadataResolver._getDependenciesMetadata ..............

I'm facing this problem too long and just solve this error by following.

Please add those two line in your 'app.component.ts' and your problem will be solved.

    import 'core-js/es6/reflect';

    import 'core-js/es7/reflect'; 
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
0

In my case, I moved guard file to another directory. Re ng serve solved my problem.

Richard
  • 556
  • 5
  • 12