动态添加路由基本功能
let routes=[{ path: '/login', name: 'login', component: () => import('../components/Login.vue') }]
this.$router.addRoutes(routes)
涉及多层路由嵌套 如图
单纯使用addRoutes 层级结构不同
修改路由结构
例:
{ name:'account', path: '/account/account', meta: { title: '个人中心', requireAuth: true }, component: account, children:[ { name: 'account', path: '/account/account', meta: { title: '账号设置', requireAuth: true }, component: setAccount, }, { name: 'childMgt', path: '/account/childMgt', meta: { title: '子账号管理', requireAuth: true }, component: childMgt, }, ] },修改单一结构
{ name:'account', path: '/account/account', meta: { title: '个人中心', requireAuth: true }, component: account, children:[ { name: 'account', path: '/account/account', meta: { title: '账号设置', requireAuth: true }, component: setAccount, }, ] }, { name:'account', path: '/account/childMgt', meta: { title: '个人中心', requireAuth: true }, component: account, children:[ { name: 'userMgt', path: '/account/childMgt', meta: { title: '子账号管理', requireAuth: true }, component: childMgt, }, ] },每一层单独包含一个子集合方便权限管理动态添加
main.js
router.beforeEach((to, from, next) => { if (from.name == null) { //页面刷新 let pathName = sessionStorage.getItem("pathName") //暂存上一个路由 if (pathName == to.path||pathName==to.redirectedFrom) { } else { sessionStorage.setItem("pathName", to.redirectedFrom) } } else { sessionStorage.setItem("pathName", to.path) } next() })app.vue
let routes=[处理后路由信息] this.$router.addRoutes(routes) this.$nextTick(i=>{ this.$router.replace(sessionStorage.getItem("pathName"))//跳转指定地址 否则404 })