我有一个 AuthGuard.service.ts 的代码,如下所示。
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;
}
}
}
在 userLoginService.ts 中,我的代码如下
export class userLoginService {
constructor() {}
IsloggedIn(): boolean {
return false;
}
}
我在我的路线中注入这个 AuthGuard.service.ts 如下。而且我还在 NgModule 的提供者中提供了这个服务名称。
const appRoutes: Routes = [
{ path: '', component: HomePageComponent, pathMatch: 'full' },
{ path: 'CartItems', component: CartItemsComponent, pathMatch: 'full', canActivate: [ AuthGuard ]},
];
@NgModule({
.
.
.
providers: [UserInfoService , AuthGuard],
.
.
.
现在执行此代码时,我收到如下错误。
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)
请让我知道我在哪里犯了错误。