Angular编译器cli v.7.2.0抛出了:属性“ runGuardsAndResolvers”的类型不兼容。类型“字符串”不可分配给类型“ RunGuardsAndResolvers”设置runGuardsAndResolvers: 'always'导入到中的常量时AppRoutingModule。当我runGuardsAndResolvers: 'always'在AppRoutingModule自己内部设置时,不会发生这种情况。app-routing.module.tsimport { NgModule } from '@angular/core';import { Routes, RouterModule } from '@angular/router';import { adminRouterConfig } from 'src/app/core/_routing/admin-router-config';const appRoutes: Routes = [ { path: '', component: HomeComponent }, { path: '', children: [ ...adminRouterConfig ] }, { path: '**', redirectTo: '', pathMatch: 'full' } ];@NgModule({ imports: [RouterModule.forRoot(appRoutes, { enableTracing: true })], exports: [RouterModule]})export class AppRoutingModule { }admin-router-config.tsimport { AuthGuard } from 'src/app/core/_guards/auth.guard';export const adminRouterConfig = [ { path: 'admin', runGuardsAndResolvers: 'always', canActivate: [AuthGuard], children: [ ... ] }];
1 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
这只是类型推断在TypeScript中的工作方式。首次将其分配给变量时,它将推断出该变量的类型{runGuardsAndResolvers: string},然后在以后使用时不匹配。在这种情况下,您必须明确指定类型:
const options = {
runGuardsAndResolvers: "always" as RunGuardsAndResolvers
};
看一下这个例子:
const method = (options: { runGuardsAndResolvers: RunGuardsAndResolvers }) => {
return options;
};
// This works
method({ runGuardsAndResolvers: "always" });
// This fails
const options = { runGuardsAndResolvers: "always" };
method(options);
// This works
const typedOptions = {
runGuardsAndResolvers: "always" as RunGuardsAndResolvers
};
method(typedOptions);
添加回答
举报
0/150
提交
取消