【问题标题】:(Angular 12) The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script(Angular 12)目标环境不支持动态 import() 语法,因此无法在脚本中使用外部类型“模块”
【发布时间】:2021-09-17 07:27:24
【问题描述】:

我有以下动态导入代码在运行时导入一个 js 库:

export class AuthService {
    constructor() {
        import('https://apis.google.com/js/platform.js').then(result => {
            console.log(result)
        })
    }
}

但是,当我尝试构建/服务 Angular 项目时,我得到:

外部“https://apis.google.com/js/platform.js” - 错误:目标 环境不支持动态 import() 语法,所以它不是 可以在脚本中使用外部类型“模块” 你的意思是构建一个 EcmaScript 模块('output.module: true')?

此外,vs-code 突出显示import 如下:

我尝试在package.json 中添加"type": "module",在tsconfig.json 中添加"module": "ESNext",但没有成功。

我该如何解决这个问题?

【问题讨论】:

  • 遇到同样的问题 - 您找到解决方案了吗?

标签: javascript angular typescript webpack


【解决方案1】:

动态导入仅适用于 ES2020。请检查您的 tsconfig.json 以了解您的目标版本。

【讨论】:

  • 我试过把目标改成ES2020/ES2021/ESNext,还是不行
猜你喜欢
  • 2022-11-22
  • 2022-09-29
  • 2022-06-19
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2020-08-25
  • 1970-01-01
  • 2020-10-10
相关资源
最近更新 更多