1.问题
Access to script at 'file:///D:/java/tomcat8-ac/webapps/web1/imt.js'
访问'file:///D:/java/tomcat8-ac/webapps/web1/imt.js'
from origin 'null' has been blocked by CORS policy:
从原点“null”已被CORS策略阻止:
Cross origin requests are only supported for protocol schemes:
跨源请求只支持协议方案:
http, data, chrome, chrome-extension, https.
2.原因分析
从原点“null”已被CORS策略阻止:
为什么被阻止,因为跨源请求不支持file协议。
解决办法:搭建一个http服务器,用支持的协议访问。
3.实验文件
index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>import/export实验</title>
</head>
<body>
<script type="module" >
import {mian} from './imt.js';
alert("ddd"+mian);
</script>
</body>
</html>
imt.js
// JavaScript Document
export var mian = 5;