为了账号安全,请及时绑定邮箱和手机立即绑定

Angular 7.2.0:类型“ string”不可分配给类型“ RunGuardsAndReso

Angular 7.2.0:类型“ string”不可分配给类型“ RunGuardsAndReso

慕慕森 2021-03-31 14:18:38
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);


查看完整回答
反对 回复 2021-04-08
  • 1 回答
  • 0 关注
  • 330 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信