类型“string”不可分配给类型“LoadChildrenCallback”

mea*_*our 14 angular-ui-router angular-material angular2-routing angular

我正在使用Angular 13并创建一个myapp.mainrouting.js文件并尝试声明loadChildren如下:

import {CustomerAppHomeComponent} from "../Home/CustomerApp.HomeComponent";
import {Routes} from "@angular/router";    

export const MainRoutes: Routes  = [
  { path: 'Home', component: CustomerAppHomeComponent },
  { path: 'Supplier', loadChildren: '../Supplier/CustomerApp.SupplierModule#CustomerAppSupplierModule' },
  { path: '', component: CustomerAppHomeComponent },
]
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

error TS2322: Type 'string' is not assignable to type 'LoadChildrenCallback | undefined'.

8   { path: 'Supplier', loadChildren: '../Supplier/CustomerApp.SupplierModule#CustomerAppSupplierModule' },
                        ~~~~~~~~~~~~

  node_modules/@angular/router/router.d.ts:1998:5
    1998     loadChildren?: LoadChildren;
             ~~~~~~~~~~~~
    The expected type comes from property 'loadChildren' which is declared here on type 'Route'
Run Code Online (Sandbox Code Playgroud)

bil*_*_ln 44

对于动态导入,您需要更新此

{ path: 'Supplier', loadChildren: '../Supplier/CustomerApp.SupplierModule#CustomerAppSupplierModule' },
Run Code Online (Sandbox Code Playgroud)

对此:

{ 
   path: 'Supplier',
   loadChildren: () => import('../Supplier/CustomerApp.SupplierModule').then(x => x.CustomerAppSupplierModule)
},
Run Code Online (Sandbox Code Playgroud)

还要仔细检查 tsconfig.json 中是否有这一行"module": "esnext"

{
...
"compilerOptions": {
    ...
    "module": "esnext"
    ...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 确保 tsconfig.app.json 文件中不包含模块 attritube (2认同)

小智 5

您之所以遇到此问题,是因为由于 Angular 的新采用,导入子模块的方式已被弃用。

改成这个格式

{ path: 'Supplier', 
  loadChildren: () => import('../Supplier/SupplierModule.module').then(m => m.SupplierModule },
  
  // *supplier/...*is supposed to be your file name and the *m... us supposed to be your module name
                        ~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

另外,请注意,您的 tsconfig.json 文件可能需要返工,将模块密钥的值更改为“esnext”。是为了支持现在使用的动态模块加载