我有以下动态导入代码在运行时导入 js 库:
export class AuthService {
constructor() {
import('https://apis.google.com/js/platform.js').then(result => {
console.log(result)
})
}
}
但是,当我尝试构建/服务 Angular 项目时,我得到:external "https://apis.google.com/js/platform.js" - Error: The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script Did you mean to build a EcmaScript Module ('output.module: true')?
此外,vs-code 突出显示
import
具有以下内容:我尝试添加
"type": "module"
在 package.json
和 "module": "ESNext"
在 tsconfig.json
,但没有运气。我怎样才能解决这个问题?
最佳答案
动态导入仅适用于 ES2020。请检查您的 tsconfig.json 以了解您的目标版本。
关于javascript - (Angular 12)目标环境不支持动态 import() 语法,因此无法在脚本中使用外部类型 'module',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68274360/