123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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
|