JavaScript module on browser [duplicate]

吃可爱长大的小学妹 提交于 2020-06-01 05:38:04

问题


catalog

 - 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');
   }

descripttion

when run index.html on browser, is don't work, and i got this error:

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.

it may work with server, how to make it work without server, moulti-module.

import a module from other. just like run with nodejs env.


回答1:


Importing a script as a module: File should export as module.exports

Please refer to the below link for working example: https://stackblitz.com/edit/js-mgwfrd



来源:https://stackoverflow.com/questions/59386512/javascript-module-on-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!