vue hash
模式下,URL
中存在'#'
,用'history'
模式就能解决这个问题。可是history
模式会出现刷新页面后,页面出现404。解决的办法是用nginx
配置一下。
在nginx
的配置文件中修改html
方法一:vue
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...nginx
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; } #.......其余部分省略 }