从JavaScript中的模块导入模块的正确方法是什么?

时间:2019-08-05 06:31:54

标签: javascript typescript

我在ML / DL项目中一直使用Python。但是,我最近在JavaScript中启动了一个项目,以可视化我的作品。

为简洁起见,我想从main.js导入模块(.js文件)。 (我已经检查过[1][2][3],但无法为我的案件找到正确的答案。)

我有如下所示的JS文件,应将其导入,以将所有帮助程序功能放入util.js

  

main.js-> tree.js,polygon.js,graph.js等...-> util.js

目前,我可以像下面那样从main.js导入模型。但是,我无法从导入的模块(util.js,...)中导入tree.js模块。

index.html

<script type="module" defer src="main.js"></script>

main.js

import Tree from './tree.js';
var tree = new Tree(...);
...

tree.js [问题!]

import * as util from './util.js'; <-- ("This is not properly working")
util.helperFunc();
...
export default Tree;

util.js

function helperFunc() { ... }
export {helperFunc, ...};

1)上面的结构是否违反了JavaScript / TypeScript约定? (对我来说,作为Python用户,这看起来很不错。)

2)如何从util.js导入tree.jstree.jsmain.js导入)?

3)我看到很多使用node.js的答案,不建议仅使用如下所示的简单服务器吗?

  

python3 -m http.server 8080

0 个答案:

没有答案
相关问题