Angular 8 - 延迟加载模块:错误 TS1323:仅当“--module”标志为“commonjs”或“esNext”时才支持动态导入

问题描述

您正在使用动态导入,因此您必须像这样更改 tsconfig.json 以将代码定位到esnext模块

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext", // add this line
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

还要确保检查 tsconfig.app.json 没有类似这样的模块和目标配置

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "src/test.ts",
    "src/**/*.spec.ts"
  ]
}

解决方法

当我将 Angular 从 7 更新到 Angular 8 时,延迟加载模块出现错误

我已经尝试了角度升级指南中的选项

进行了以下更改:

    loadChildren: '../feature/path/sample- 
                         tage.module#SameTagModule'

   loadChildren: () => import('../feature/path/sample- 
                      tags.module').then(m => m.CreateLinksModule)

错误 TS1323:仅当“–module”标志为“commonjs”或“esNext”时才支持动态导入。