vue路由history模式刷新页面时页面丢失时常见的两种解决方法

方法一:

1

2

3

4

5

6

7

8

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/

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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;

      }

      #.......其他部分省略

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章