首頁  >  文章  >  web前端  >  vue路由history模式刷新頁面時出現404問題的兩種解決方法

vue路由history模式刷新頁面時出現404問題的兩種解決方法

不言
不言轉載
2018-10-12 16:48:5912738瀏覽
這篇文章帶給大家的內容是關於vue路由history模式刷新頁面時出現404問題的兩種解決方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

vue hash模式下,URL中存在'#',用'history'模式就能解決這個問題。但是history模式會出現重新整理頁面後,頁面出現404。解決的方法是用nginx配置。

在nginx的設定檔中修改

方法一:

location /{
    root   /data/nginx/html;
    index  index.html index.htm;
    if (!-e $request_filename) {
        rewrite ^/(.*) /index.html last;
        break;
    }
}

方法二:
vue.js官方教學裡提到的https://router.vuejs.org/zh/g...

  server {
        listen       8081;#默认端口是80,如果端口没被占用可以不用修改
        server_name  myapp.com;
        root        D:/vue/my_app/dist;#vue项目的打包后的dist
        location / {
            try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
            index  index.html index.htm;
        }
        #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
        #因此需要rewrite到index.html中,然后交给路由在处理请求资源
        location @router {
            rewrite ^.*$ /index.html last;
        }
        #.......其他部分省略
  }

#

以上是vue路由history模式刷新頁面時出現404問題的兩種解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除