报错信息如下:
Access to script at 'file:///D:/%E5%9B%9B%E5%B7%9D%E5%86%9C%E4%B8%9A%E5%A4%A7%E5%AD%A6/%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88/%E9%9D%A2%E8%AF%95%E7%9B%B8%E5%85%B3/%E9%9D%A2%E8%AF%95%E7%9B%B8%E5%85%B3%E7%BB%83%E4%B9%A0/2022%E6%9C%80%E6%96%B0%E9%9D%A2%E8%AF%95%E9%A2%98-%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9/code/%E9%87%91%E6%B8%A1%E6%95%99%E8%82%B2/vue%E9%9D%A2%E8%AF%95%E9%A2%98/07-%E7%9B%91%E5%90%AC%E5%99%A8%E7%9A%84%E5%AE%9E%E7%8E%B0/objServer.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
引入方式示例:
<script type="module">
import { Objserver } from './objServer.js'
let obj = new Objserver({
name:'张三',
age:18
})
console.log(obj.value.name);
</script>
open in defaule browser 在浏览器打开:
分析原因:由于module引入的方式,会产生跨域请求,本地file协议不支持,无法加载对应js模块。
解决方法1:
vscode中安装live Server插件并Open with Live Server运行
效果如下:
解决方法2:
在项目目录终端安装anywhere:npm install anywhere -g 并在终端输入anywhere
选择需要运行的文件
效果如下所示:
各位大佬们还有其它解决办法,欢迎打在评论区,一起学习!