router.beforeEach((to, from, next) => {
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
next({ path: '/' })
} else {
if (!store.getters.name) {
setTimeout(() => {
let divRouter = [
{
path: '/example',
component: 'Layout',
redirect: '/example/table',
name: 'Example',
meta: { title: '举个栗子', icon: 'el-icon-s-help' },
redirect: 'noRedirect',
children: [
{
path: 'table',
name: 'Table',
component: () => import('@/views/table/index'),
meta: { title: 'Table222', icon: 'table' }
},
{
path: 'tree',
name: 'Tree',
component: () => import('@/views/tree/index'),
meta: { title: 'Tree', icon: 'tree' }
}
]
},
]
divRouter.forEach(item => {
if (item.component && item.component === 'Layout') {
item.component = Layout
}
})
router.options.routes.push(...divRouter);
router.addRoutes(divRouter)
store.commit('user/SET_NAME', '晨淘淘')
next({ ...to, replace: true })
}, 500)
} else {
next()
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next()
NProgress.done()
}
}
})
用的vue-admin-template模板代码。
|