浏览器上的JavaScript模块 [英] JavaScript module on browser

查看:120
本文介绍了浏览器上的JavaScript模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目录

 - folder
  - index.html
  - index.js
  - index1.js

index.html

index.html

   <!-- main content --->
   <script type='module' src='./index.js'></script>

index.js

   import {show} from "./index1"

   show();

index1.js

index1.js

   export default function show(){
       console.log('hello world');
   }

说明

在浏览器上运行index.html时不起作用,并且出现此错误:

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.

它可以与服务器一起使用,如何使其在没有服务器的情况下运行,而必须使用moulti模块.

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

从其他导入模块.就像与nodejs env一起运行.

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

推荐答案

将脚本导入为module:文件应导出为module.exports

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

有关工作示例,请参阅以下链接: https://stackblitz.com/edit/js-mgwfrd

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

这篇关于浏览器上的JavaScript模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆