简体   繁体   English

将一个JavaScript文件导入另一个JavaScript文件导入另一个文件

[英]Import a JavaScript file into another JavaScript file that is imported into another file

I need to import a file that imports code from another file and then import that file into a final file that adds code to an HTML button.我需要导入一个从另一个文件导入代码的文件,然后将该文件导入到最终文件中,该文件将代码添加到 HTML 按钮。 How can I do this?我怎样才能做到这一点? When I try with the following example below no code runs.当我尝试使用下面的示例时,没有代码运行。

// file 1
export function someFunction() {
    // more code
}

// file 2
import {someFunction} from './file1';
export function otherFunction() {
    someFunction();
    // more code
}

// file 3
import {otherFunction} from './file2';
document.getElementById('some-button').onclick = otherFunction;

// in index.html 
<script type='module' src='./file3.js'></script>

The solution is to have all the files be included in the index.html as type="module"解决方案是将所有文件作为 type="module" 包含在 index.html 中

<script type='module' src='./file1.js'></script>
<script type='module' src='./file2.js'></script>
<script type='module' src='./file3.js'></script>

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

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