【问题标题】:JavaScript module on browser [duplicate]浏览器上的 JavaScript 模块 [重复]
【发布时间】:2020-04-10 16:20:19
【问题描述】:

目录

 - 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.

它可以与服务器一起工作,如何使它在没有服务器的情况下工作,多模块。

从其他导入一个模块。就像使用 nodejs env 运行一样。

【问题讨论】:

标签: javascript ecmascript-6 module


【解决方案1】:

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

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

【讨论】:

  • 对不起,你的demo运行正常,但是在localhost上不行,报错信息Access to script at 'file:///Users/br/Documents/code/demo_canvas/demo10/fileHandler.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.
猜你喜欢
  • 2017-02-23
  • 2012-05-02
  • 2018-12-29
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 2019-02-07
相关资源
最近更新 更多