import Vue from 'vue'
|
import Router from 'vue-router'
|
import { asyncRouterMap, constantRouterMap } from '@/config/router.config'
|
|
// hack router push callback
|
const originalPush = Router.prototype.push
|
Router.prototype.push = function push(location, onResolve, onReject) {
|
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
|
return originalPush.call(this, location).catch((err) => err)
|
}
|
|
Vue.use(Router)
|
|
const createRouter = () =>
|
new Router({
|
mode: 'hash',
|
routes: constantRouterMap,
|
})
|
|
const router = createRouter()
|
|
// 定义一个resetRouter 方法,在退出登录后或token过期后 需要重新登录时,调用即可
|
export function resetRouter() {
|
const newRouter = createRouter()
|
router.matcher = newRouter.matcher
|
}
|
router.$addRoutes = (params) => {
|
router.matcher = new Router({ mode: 'hash' }).matcher
|
router.addRoutes(params)
|
}
|
|
// 公共路由
|
const constantRoutes = [
|
{
|
path: '/dashboard',
|
name: 'dashboard',
|
component: () => import('@/views/dashboard/Workplace'),
|
meta: {
|
title: '工作台',
|
},
|
hidden: true,
|
},
|
{
|
path: '/404',
|
component: () => import('@/views/exception/404'),
|
meta: {
|
title: '404',
|
},
|
hidden: true,
|
},
|
]
|
// // 清空路由表
|
// export const asyncRouterMap = [];
|
|
// const createRouter = () =>
|
// new Router({
|
// // mode: 'history', // require service support
|
// scrollBehavior: () => ({ y: 0 }),
|
// routes: constantRoutes,
|
// });
|
|
// const router = createRouter();
|
|
// // 重置路由,防止重复添加崩溃
|
// export function resetRouter() {
|
// const newRouter = createRouter()
|
// router.matcher = newRouter.matcher // reset router
|
// }
|
export default router
|