最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (Angular 12) The target environment doesn't support dynamic import() syntax so it's not possible to

programmeradmin3浏览0评论

I have the following dynamic import code to import a js library at runtime:

export class AuthService {
    constructor() {
        import('.js').then(result => {
            console.log(result)
        })
    }
}

However when I try to build/serve the Angular project I get:

external ".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')?

In addition, vs-code highlights the import with the following:

I tried adding "type": "module" in package.json and "module": "ESNext" in tsconfig.json, but with no luck.

How can I fix this?

I have the following dynamic import code to import a js library at runtime:

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

However when I try to build/serve the Angular project I get:

external "https://apis.google./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')?

In addition, vs-code highlights the import with the following:

I tried adding "type": "module" in package.json and "module": "ESNext" in tsconfig.json, but with no luck.

How can I fix this?

Share Improve this question asked Jul 6, 2021 at 16:38 Two HorsesTwo Horses 1,7123 gold badges19 silver badges43 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2

Dynamic imports only works since ES2020. Please, check your tsconfig.json in order to know what version is your target.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论