Angular 8 - 延迟加载模块:错误 TS1323:仅当“--module"标志为“commonjs"或“esNext"时才支持动态导入 [英] Angular 8 - Lazy loading modules : Error TS1323: Dynamic import is only supported when '--module' flag is 'commonjs' or 'esNext'

查看:43
本文介绍了Angular 8 - 延迟加载模块:错误 TS1323:仅当“--module"标志为“commonjs"或“esNext"时才支持动态导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

进行了以下更改:

之前

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

之后

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

<块引用>

错误 TS1323:仅当 '--module' 标志为时才支持动态导入'commonjs' 或 'esNext'.

解决方案

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

<代码>{compileOnSave":假,编译器选项":{"baseUrl": "./","outDir": "./dist/out-tsc","sourceMap": 真,声明":假,"module": "esnext",//添加这一行"moduleResolution": "节点",emitDecoratorMetadata":真,"experimentalDecorators": 真,importHelpers":真,"目标": "es2015",类型根":[节点模块/@types"],库":["es2018",dom"]}}

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

<代码>{"扩展": "./tsconfig.json",编译器选项":{"outDir": "./out-tsc/app",类型":[]},包括": ["src/**/*.ts"],排除": ["src/test.ts","src/**/*.spec.ts"]}

When I updated Angular from 7 to Angular 8, getting error for lazy loading modules

I have tried the options, which are there in the angular upgradation guide

Made the below changes:

Before

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

After

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

error TS1323: Dynamic import is only supported when '--module' flag is 'commonjs' or 'esNext'.

解决方案

You are using dynamic import so you have to change your tsconfig.json like this to target your code to esnext module

{
  "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"
    ]
  }
}

Also make sure to check tsconfig.app.json dont have module and target config something like this

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

这篇关于Angular 8 - 延迟加载模块:错误 TS1323:仅当“--module"标志为“commonjs"或“esNext"时才支持动态导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆