Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resolve all parameters for AuthGuard: In my AuthGuard service

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.

like image 743
roopteja Avatar asked Dec 06 '17 17:12

roopteja


1 Answers

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 {
    ...
}
like image 182
Paris Benamour Avatar answered Oct 17 '22 12:10

Paris Benamour