目录
- folder
- index.html
- index.js
- index1.js
index.html
<!-- main content --->
<script type='module' src='./index.js'></script>
index.js
import {show} from "./index1"
show();
index1.js
export default function show(){
console.log('hello world');
}
描述
当在浏览器上运行index.html时,不起作用,并且我收到此错误:
Access to script at 'file:///Users/xxx/Documents/code/canvas/test/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
它可以与服务器一起工作,如何使其在没有服务器的情况下工作,moulti-module。
从其他模块导入模块。就像使用 nodejs env 运行一样。
最佳答案
将脚本导入为模块
:文件应导出为module.exports
工作示例请引用以下链接: https://stackblitz.com/edit/js-mgwfrd
关于浏览器上的 JavaScript 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59386512/