参考链接
hash模式带#,这个服务器不会解析,相对于还是返回html而已,index.html会根据vue路由去解析;
history模式则会请求服务器上的地址,服务器上没有相关路径就会报错了,vue-router的官方文档有介绍各种配置;
下面说一下我的解决方案:
如果项目不在根目录下,路由需添加配置(base)
router/index.js
mode: 'history',
base: '/h5/',
config/index.js
build: {
assetsPublicPath: '/h5/'
}
重要!!!后台配置代码:
// 如果项目代码在根目录
location / {
try_files $uri $uri/ /index.html;
}
// 项目代码不在根目录,前端路由需添加 base 路径,例如:base: '/h5/'
location /history {
root C:/web/static;
index index.html index.htm;
#error_page 404 /history/index.html;
if (!-e $request_filename) {
rewrite ^/(.*) /history/index.html last;
break;
}
}
END.