网上找了好多,大部分说的是将
{ path: '*', redirect: '/404', hidden: true }放在路由最后就可实现,但亲测这种方式对history方式的路由配置无效,history需要使用下面的方法来实现,已经验证过线上是可行的

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: Routes
})

history方式需要服务端支持,浏览器地址栏的路径在刷新时会请求服务端,但服务端找不到文件会返回404,所以服务端nginx要增加重定向,将请求重新定位回index页面
nginx完整配置如下:

server {
        listen      80;
        server_name mydomain.com;
        root /wnmp/www;
        index  index.html index.htm index.php;
        
	#增加重定向回index.html页面
        location / {
                try_files $uri $uri/ /index.html;
                index index.html;
        }
}
GitHub 加速计划 / vu / vue-admin-template
16
5
下载
PanJiaChen/vue-admin-template: 基于 Vue.js 和 Element UI 的后台管理系统模板,支持多语言、主题和布局切换。该项目提供了一个完整的后台管理系统模板,可以方便地实现后台管理系统的快速搭建和定制,同时支持多种数据源和插件扩展。
最近提交(Master分支:4 个月前 )
4c18a3f4 - 3 年前
714ded11 - 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐