如何将导入和导出与此简单代码一起使用



我想使用index.js文件中的doAlert函数。现在,在这种情况下,我该如何使用导入和导出?

//index.html
<script type="module" src="index.js"></script>
//index.js
/*'index.js' is an empty file.*/
//doAlert.js
function doAlert() {
alert('Hello');
}
/* index.html */
<script type="module" src="index.js"></script>
<script type="module" src="doAlert.js"></script>
/* doAlert.js */
export function doAlert() {
alert('Hello');
}
/* index.js */
import { doAlert } from './doAlert.js';
// use it
doAlert();

关于JS模块的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

最新更新