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