import { createRouter, createWebHashHistory, Router } from 'vue-router' import Layout from '@/layout' import { RouterTy } from '~/router' import routesOne from './routes/routes-file-one' import routesTwo from './routes/routes-file-two' import routesThree from './routes/routes-file-three' import routesFour from './routes/routes-file-four' import routesFive from './routes/routes-file-five' import routesSix from './routes/routes-file-six' import { Page, useKeepAlive } from '@/store/keepAlive' const routesPush = (arr, routes) => { if (!Array.isArray(arr)) return '请传入数组格式路由表' for (let i = 0; i < arr.length; i++) { routes.push(arr[i]) } } export const constantRoutes: RouterTy = [ { path: '/redirect', component: Layout, hidden: true, children: [ { path: '/redirect/:path(.*)', component: () => import('@/views/redirect'), }, ], }, { path: '/login', component: () => import('@/views/login/Login.vue'), hidden: true, }, { path: '/404', component: () => import('@/views/error-page/404.vue'), hidden: true, }, { path: '/401', component: () => import('@/views/error-page/401.vue'), hidden: true, }, ] /** * asyncRoutes * the routes that need to be dynamically loaded based on user roles */ export const asyncRoutes: RouterTy = [ // 404 page must be placed at the end !!! // using pathMatch install of "*" in vue-router 4.0 { path: '/:pathMatch(.*)', redirect: '/404', hidden: true }, ] // 插入路由 routesPush( [ ...routesOne, ...routesSix, ...routesFive, ...routesTwo, ...routesThree, ...routesFour, ], asyncRoutes ) const router: Router = createRouter({ history: createWebHashHistory(), scrollBehavior: () => ({ top: 0 }), routes: constantRoutes, }) router.beforeEach((to, from, next) => { const { savedPages, savePage } = useKeepAlive() if (from.matched?.[0]?.name === 'RealTime') { if ( typeof from.name === 'string' && ((from.name.includes('FlightView') && from.query.flightNO && from.query.flightDate) || (from.name.includes('WaybillView') && from.query.waybillNO)) ) { savePage(from as Page) } } if (to.matched?.[0]?.name === 'RealTime') { if ( typeof to.name === 'string' && ((to.name.includes('FlightView') && (!to.query.flightNO || !to.query.flightDate)) || (to.name.includes('WaybillView') && !to.query.waybillNO)) ) { const savedPage = savedPages.find(savedPage => savedPage.name === to.name) if (savedPage) { next(savedPage.fullPath) } } } next() }) // export function resetRouter() { // const newRouter = createRouter({ // history: createWebHashHistory(), // scrollBehavior: () => ({ top: 0 }), // routes: constantRoutes // }) // } export default router