无法解析 AuthGuard 的所有参数
Can't resolve all parameters for AuthGuard
我有这个AuthGuard
:
export class AuthGuard implements CanActivate {
constructor (private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (localStorage.getItem('token')) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}
而且我已经在我的AppModule
中提供了它:
@NgModule({
declarations: [/*...*/],
imports: [NotificationRoutingModule],
providers: [AuthService, AuthGuard],
bootstrap: [AppComponent]
})
export class AppModule {
}
我试着在我的路由模块中使用这个守卫,就像这样:
const routes = [
{
path: 'notification',
component: NotificationRootComponent
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: NotificationListComponent, canActivate: [AuthGuard]},
{path: ':id', component: NotificationDetailComponent, canActivate: [AuthGuard]}
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
declarations: []
})
export class NotificationRoutingModule {
}
我得到这个错误:
我错过了什么吗?这个错误的根源在哪里?
编辑:
我正在使用 Angular 4.4.6
当我使用基本路由守卫时它工作正常,并更新了路由配置:
@NgModule({
declarations: [],
imports: [],
providers: [
{
provide: 'CannotBeActivated',
useValue: () => {
return false;
}
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
const routes = [
{
path: 'notification',
component: NotificationRootComponent,
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: NotificationListComponent, canActivate: ['CannotBeActivated']},
{path: ':id', component: NotificationDetailComponent, canActivate: ['CannotBeActivated']}
]
}
];
**我的tsconfig.json:**
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
您只是错过了将 @Injectable() 放在您的 AuthGuard 服务之前
我有这个AuthGuard
:
export class AuthGuard implements CanActivate {
constructor (private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (localStorage.getItem('token')) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}
而且我已经在我的AppModule
中提供了它:
@NgModule({
declarations: [/*...*/],
imports: [NotificationRoutingModule],
providers: [AuthService, AuthGuard],
bootstrap: [AppComponent]
})
export class AppModule {
}
我试着在我的路由模块中使用这个守卫,就像这样:
const routes = [
{
path: 'notification',
component: NotificationRootComponent
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: NotificationListComponent, canActivate: [AuthGuard]},
{path: ':id', component: NotificationDetailComponent, canActivate: [AuthGuard]}
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
declarations: []
})
export class NotificationRoutingModule {
}
我得到这个错误:
我错过了什么吗?这个错误的根源在哪里?
编辑: 我正在使用 Angular 4.4.6
当我使用基本路由守卫时它工作正常,并更新了路由配置:
@NgModule({
declarations: [],
imports: [],
providers: [
{
provide: 'CannotBeActivated',
useValue: () => {
return false;
}
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
const routes = [
{
path: 'notification',
component: NotificationRootComponent,
children: [
{path: '', redirectTo: 'list', pathMatch: 'full'},
{path: 'list', component: NotificationListComponent, canActivate: ['CannotBeActivated']},
{path: ':id', component: NotificationDetailComponent, canActivate: ['CannotBeActivated']}
]
}
];
**我的tsconfig.json:**
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
您只是错过了将 @Injectable() 放在您的 AuthGuard 服务之前