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

Angular编译器cli v.7.2.0抛出了:


属性“ runGuardsAndResolvers”的类型不兼容。类型“字符串”不可分配给类型“ RunGuardsAndResolvers”


设置runGuardsAndResolvers: 'always'导入到中的常量时AppRoutingModule。


当我runGuardsAndResolvers: 'always'在AppRoutingModule自己内部设置时,不会发生这种情况。


app-routing.module.ts


import { 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.ts


import { AuthGuard } from 'src/app/core/_guards/auth.guard';


export const adminRouterConfig = [

  {

    path: 'admin',

    runGuardsAndResolvers: 'always',

    canActivate: [AuthGuard],

    children: [

      ...

    ]

  }

];


慕慕森
浏览 331回答 1
1回答

qq_花开花谢_0

这只是类型推断在TypeScript中的工作方式。首次将其分配给变量时,它将推断出该变量的类型{runGuardsAndResolvers: string},然后在以后使用时不匹配。在这种情况下,您必须明确指定类型:const options = {  runGuardsAndResolvers: "always" as RunGuardsAndResolvers};看一下这个例子:const method = (options: { runGuardsAndResolvers: RunGuardsAndResolvers }) => {  return options;};// This worksmethod({ runGuardsAndResolvers: "always" });// This failsconst options = { runGuardsAndResolvers: "always" };method(options);// This worksconst typedOptions = {  runGuardsAndResolvers: "always" as RunGuardsAndResolvers};method(typedOptions);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript