当前位置: 首页 > 知识库问答 >
问题:

前端 - vue3增加动态路由 刷新页面404是什么问题?

方航
2024-08-11

目前是开发环境,history模式,有个index.vue的主页加了路由组件<RouterView/>,在路由导航守卫中向这个页面动态加了子路由,点击菜单的时候,跳转到了对应页面,但刷新后会打到404页面,后面发现是加了这个导致的“path: "/:catchAll(.*)",redirect: '/404',” 如果不加则正常,但是控制台会出现警告[Vue Router warn]: No match found for location with path,而且如果url输入一个不存在的路径也不会跳转到404了

index.vue

<el-container>
      <el-header style="padding: 0;height: 100px">
        <Header></Header>
      </el-header>
      <el-main>
        <RouterView/>
      </el-main>
      <el-footer>Footer</el-footer>
    </el-container>
const createBaseRouter = () => createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {path: '/', redirect: '/login'},
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/index',
            name: 'Index',
            component: Index,
            children: []
        },
        {
            path: '/404',
            // name: '404',
            component: () => import('@/components/error/404.vue')
        },
        {
            path: '/401',
            name: '401',
            component: () => import('@/components/error/401.vue')
        },
        {
            // vue2使用* vue3使用:pathMatch('*') 或者 :pathMatch('*')* 或者 :catchAll(.*)
            path: "/:catchAll(.*)",
            redirect: '/404',
        }
    ]
})
// 路由前置守卫
router.beforeEach( (to, from, next) => {
    if (to.path === '/login') return next();
    let token = getToken();
    if (!token) {
        return next('/login');
    } else {
        if (Object.keys(useUserStore().userInfo).length === 0) {//如果没有用户,则获取用户信息
            getUserInfo().then(res => {
                useUserStore().setUserInfo(res.data)
                useUserStore().setPerms(res.data.permsList)
                useUserStore().setMenuTreeList(res.data.menuTreeList)
                const routes = router.getRoutes()
                let indexRoute: any = routes.find(r => r.name === 'Index')
                const dynamicsRoutes =  filterAsyncRouter(res.data.menuTreeList)
                // 添加路由
                indexRoute.children = indexRoute.children.concat(dynamicsRoutes);
                router.addRoute(indexRoute)
                console.log(router.getRoutes());
                next({...to, replace: true});
            }).catch((e) => {
                ElMessage.error(e.message);
            });
        } else {
            next();
        }
    }
})

共有2个答案

苏培
2024-08-11

不要重定向到404,写成下面试试

{
    path: "/:catchAll(.*)*",
    component: () => import('@/views/error/404'),
}
小牛23153
2024-08-11

路由改成hash模式即可createWebHashHistory

import { createRouter, createWebHashHistory } from 'vue-router'
const createBaseRouter = () => createRouter({
    history: createWebHashHistory(),
    ...
})
 类似资料:
  • 我想写一个vue3动态路由的demo,写完之后,在浏览器上访问/home或者/system均是白屏,只有/login是可以正常访问的,这段代码到底是哪里出现了问题? 这是我的路由数据: 这是我的代码: 控制台打印console.log(router.getRoutes())已经看到路由已经添加了进去,但是浏览器访问对应的路由显示空白,报错提示没有匹配的路由,这是哪个地方有问题?

  • 想通过v-router路由守卫和vuex实现一个动态路由,路由数据由后端返回,求一个demo参考下。

  • 最近在使用vue-admin-template开发后台管理系统,遇到动态路由刷新出现页面白屏问题 用户登录时通过角色获取角色路由并动态加载到router对象 在App.vue 中将保存于vuex中的路由信息刷新前存入到sessionStorage中,刷新后取出并重新加载路由; 重置路由方法 全局路由守卫 角色路由直接编写在JS文件中 出现问题正常登录能够进行路由跳转但是点击浏览器刷新出现页面白屏,

  • react中使用createBrowserRouter 路由模式,在本地一切正常,但是打包到线上,可以点击链接去访问,但刷新后就报404(只要一刷新当前请求URL非根路径,就会报错404。) 使用createBrowserRouter 路由模式解决404问题

  • 这个所属公司里面我想动态添加几个从接口获取的二级行业并且生成对应的路由点击跳转不同的页面,这个在vue3和vite项目中要怎么实现呢 就比如这种效果 我想的是先通过rouer.option.route获取route,然后再拿到这个所属公司这个route,其次再获取接口拿到接口数据,通过addroute添加进去路由里面,然后我试了下这个方案不太行,而且会重复添加,我也不能设置一个变量去防止他重复添加

  • 问题:vue添加动态路由,没有被添加为动态路由的页面能在地址栏访问 原因:清除缓存、用户信息、pinia状态都试了还是能访问,当我使用router.getRoutes()获取动态路由列表,居然出现在动态路由列表。都没添加这个路由页面,为什么会在动态路由列表中?有大佬指点吗? 打印:console.log(route) 这个route是对比筛选出有权限的路由,其中没有/basedata/device

  • router.getRoutes()数据如下。 页面报错No match found for location with path "/a" 路由跳转:空白页面。

  • 怎么强制用户刷新页面? 今天遇到一个问题,是一个公众号上面的项目,修改了一个问题,但是需要用户刷新才能看到效果?用什么技术可以实现?无论是小程序,公众号还是h5,只要我上传了修改过的代码,就可以让用户直接看到效果,不管用户是否强制刷新? 无论是小程序,公众号还是h5,只要我上传了修改过的代码,就可以让用户直接看到效果,不管用户是否强制刷新?