uniapp打包成h5,本地服务器运行,路径报错问题
首先需要去manifest.json中的web配置中配置
配置完成直接去打包
打包后的文件路径
打包完后的index.html文件不能直接运行,可以看到控制台有file//…等路径错误,原因如下:
报错提示:Access to script at ‘file://…’ from origin ‘null’ has been blocked by CORS policy
原因:
- 浏览器认为 file:// 协议的网页没有安全来源(origin 是 null)
- 浏览器默认阻止从 file:// 协议加载其他 file:// 资源
- 所以即使文件存在,也会报跨域错误
这属于 浏览器安全限制,不是 uniapp 或你的路径错误。
解决方案,远程服务器部署和本地部署启动调试
(推荐)方法 1:使用 node 的 http-server
下载:npm install -g http-server
运行(切换到打包文件的根目录下面):http-server -c-1
访问:http://localhost:8080/index.html
指定端口运行(3000):http-server -c-1 -p 3000
方法 2:使用 Python
Python 3.x
python -m http.server 8080