gpt4 book ai didi

angular - 无法解析 AuthGuard 的所有参数

转载 作者:太空狗 更新时间:2023-10-29 17:32:23 24 4
gpt4 key购买 nike

我有这个 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 {
}

我得到这个错误:

enter image description here

我错过了什么吗?这个错误的来源在哪里?

编辑:我正在使用 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"
]
}
}

最佳答案

您只是错过了在 AuthGuard 服务之前放置 @Injectable()

关于angular - 无法解析 AuthGuard 的所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com