Web前端 教程 ·

Vue设置路由为History模式,打包后访问404问题

一般情况下开发的单页应用的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; 
}

参与评论