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.
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 {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With