简体   繁体   English

typescript 中的导入语句

[英]import statement in typescript

I have 3 different TS file and loading 2 TS file dynamically as below in 1 main TS file我有 3 个不同的 TS 文件并在 1 个主 TS 文件中动态加载 2 个 TS 文件,如下所示

if(x==='xyz'){
  import('../../common1').then((common)=>{
    common.loadContent()
  })
} else if(x==='abc'){
  import('../../common2').then((common)=>{
    common.loadContent()
  })
}

Now in both TS file I am importing jquery and also import jquery parent TS file现在在两个 TS 文件中,我正在导入 jquery 并导入 jquery 父 TS 文件

import * as $ from "jquery"

My tsconfig.json我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "typeRoots": ["./node_modules/@types", "./node_modules/@microsoft"],
    "types": ["es6-promise", "webpack-env"],
    "lib": ["es5", "dom", "es2015.collection"]
  }
}

So my question is do jquery file will load 3 times or only once.所以我的问题是 jquery文件将加载 3 次或仅加载一次。

Webpack is a bit unpredictable in this case, I'm struggling with it myself , but I don't think it will be loaded multiple times. Webpack 在这种情况下有点不可预测, 我自己也在苦苦挣扎,但我认为它不会被多次加载。 After the first time it is cached and will be taken from the module cache.第一次缓存后,将从模块缓存中取出。 The only important thing is that you set the module type in the tsconfig.json to "esnext", what you have done.唯一重要的是您将 tsconfig.json 中的module类型设置为“esnext”,您所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM