vue路由history模式刷新页面一片空白,也不报错(解决方案)

参考链接
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.


版权声明:本文为weixin_45295253原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。