Vue项目 刷新出现404问题

vue页面访问正常,但是只要一刷新就会404的问题有以下两种解决办法:

1、将vue路由模式mode: 'history' 修改为 mode: 'hash'

        将用于路由的js文件里面的mode值改下就行,例如我的js文件如下图

        

//index.js文件
const router = new Router({
    //mode: 'history', 
    mode: 'hash',
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
    ]
})

2、在服务器Nginx配置文件里,添加如下代码,再刷新就OK了

location / {
 try_files $uri $uri/ @router;
  index index.html;
}
 
location @router {
  rewrite ^.*$ /index.html last;
}

猜你喜欢

转载自blog.csdn.net/weixin_47406082/article/details/132613033