Vue router dynamic routing changes to a blank page after refreshing

vue router adds dynamic route, route jumps normally, and changes to blank page after F5 refresh

sources of topics and their own ideas

related codes

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    initMenu(router, store)
    if (to.path === "/login") {
      next({ path: "/" })
      NProgress.done() // if current page is dashboard will not trigger    afterEach hook, so manually handle it
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next("/login")
      NProgress.done()
    }
  }
})
 

export const initMenu = (router, store) => {
  if (store.state.security.routes.length > 0) {
    return null
  }
  if (!util.getObjArr("router")) {
    return null
  } else {
    var getRouter = formatRoutes(util.getObjArr("router"))// 
    getRouter.push({ path: "*", redirect: "/404", hidden: true })
    const newRouter = constantRouterMap.concat(getRouter)
    router.addRoutes(newRouter)
    console.log(newRouter)
    store.commit(types.SET_ROUTES, newRouter)
  }
}

export const formatRoutes = (routes) => {
  const fmRoutes = []
  routes.forEach(router => {
    const path = router.path
    let component
    if (router.component === "Layout") {
      component = Layout
    } else {
      component = _import(router.component)
    }
    const name = router.name
    const icon = router.icon
    let children = router.children
    if (children && children instanceof Array) {
      children = formatRoutes(children)
    }
    var fmRouter = {
      path: path,
      component: component,
      name: name,
      // icon: icon,
      meta: { title: name, icon: icon },
      children: children
    }
    fmRoutes.push(fmRouter)
  })
  return fmRoutes
}

what result do you expect? What is the error message actually seen?

Jul.03,2021

next ({.to, replace: true}) is required after router.addRoutes (newRouter)


I also encountered the same problem. After logging in, the dynamic routing page this.$router.push ({path:'/page1/list'}) is normal, but the refresh is blank

.

define regular and dynamic routes in router.js:

export const constantRouter = [
    {
        path: '/login',
        name: 'login',
        hidden: true,
        component: () => import('@/views/login')
    },

    {   path:'/home',
        name:'home',
        meta:{ title:'list',icon:''},
        component: () => import('@/views/home'),
    },
    { path: '/', redirect: '/login', hidden: true }
]

export default new Router({
    routes: constantRouter
})

export const asyncRouter = [
    {
        path: '/page1',
        component: layout,
        alwaysShow: true, // will always show the root menu
        name: 'page1',
        redirect: '/page1/list',
        meta:{ title:'list',icon:''},
        children: [
            {
                path: 'list',
                component: () => import('@/views/list'),
                name:'list',
                meta:{ title:'list',icon:''},
            },
            {
                path: 'edit',
                component: () => import('@/views/form'),
                name:'edit',
                hidden:true,
                meta:{ title:'edit',icon:''},
            },
            {
                path: 'detail',
                component: () => import('@/views/detail'),
                name:'detail',
                hidden:true,
                meta:{ title:'detail',icon:''},
            },
        ]
    },
    {
        path: '/page1',
        component: layout,
        name: 'page2',
        meta:{ title:'list',icon:''},
        children: [
            {
                path: '/test',
                component: () => import('@/views/test'),
                name:'test',
                meta:{ title:'test',icon:''},
            },
        ]

    },
    {   path:'*',
        name:'errorPage404',
        hidden: true,
        component: () => import('@/views/404'),
    }
]

then addRouter: in main.js

import router from './router'
router.beforeEach((to, from, next) => {
    if(to.path !== '/login'){
        if(sessionUtil.getItem('recombineRouters')){
            next()
        }else{

            let privileges = ['list','edit'];
            let permission_routes = routeUtil.GenerateRoutes(privileges);
            router.addRoutes(permission_routes);
            console.log(router);
            next({ ...to, replace: true })
        }
    }else{
        next();
    }
})

the dynamic routing method GenerateRoutes: of Filter

function GenerateRoutes(privileges){
    var f = item => {
        if(item.path === '*'){
            return true;
        }else{
            if(item.children && item.children.length > 0){
                item.children = item.children.filter(f);
                if(item.children.length>0){
                    item.redirect = item.children[0].path;
                    return true;
                }else{
                    return false;
                }
            }else{
                return (privileges.indexOf(item.name) > -1)
            }
        }
    }

    let accessedRouters = asyncRouter.filter(f);
    let recombineRouters = constantRouter.concat(accessedRouters);
    sessionUtil.setItem('recombineRouters',recombineRouters);
    return accessedRouters;
}

first of all, my * (404) is written after dynamic routing. Secondly, instead of using next (), I use next ({.to, replace: true}) instead of addRouter, but it still doesn't solve the problem. My router is v3.0.1

.

the same problem occurs


needs backend support. Official instructions:
ide/essentials/history-mode.html-sharphtml5-history-%E6%A8%A1%E5%BC%8F" rel=" nofollow noreferrer "> Vue HTML5 History mode


have you solved it later


have you solved it later

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e62eab-446a3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e62eab-446a3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?