一般情况下开发的单页应用的URL都会带有#号的hash模式,因为业务需求,或者不想使用带#号,我们通常在路由index.js里面设置:
export default new Router({
mode: 'history',
这样URL不再会有#号,在Dev开发阶段一切都是正常的,可是在使用npm run build打包发布后页面显示一片空白和刷新项目路径出现404错误。
解决方法如下:
1、apache需要配置httpd.conf
打开文件在最后添加:
ErrorDocument 404 /index.html
2、nginx需要配置nginx.conf
打开文件修改成如下:
# 路径是根目录时
location @router {
rewrite ^.*$ /index.html last;
}
location / {
try_files $uri $uri/ @router;
index index.html;
}
3、IIS解决方案
3.1、更新IIS版本,最好的6以上的IIS,否则可能装不上。
3.2、下载web程序安装平台
地址:https://www.iis.net/downloads/microsoft/url-rewrite
3.3、安装完成后,打开IIS