123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import {
- createRouter,
- createWebHistory,
- createWebHashHistory,
- } from 'vue-router';
- import {
- ElMessage
- } from 'element-plus';
- import NProgress from 'nprogress'; //页面顶部加载进度条
- import 'nprogress/nprogress.css';
- //此处注释store
- import {
- Store
- } from '@/store/index';
- NProgress.configure({
- showSpinner: false
- });
- var store = new Store()
- // 公共路由
- export const constantRoutes = [
- /*
- *** 添加router路由
- *** 11.23日修改
- */
- {
- path: '/home',
- component: () => import('/src/views/Main/MainView.vue'),
- children: [{
- path: 'Map3d',
- name: 'Map3d',
- component: () => import('../views/Map3d/Map3DMain.vue'),
- meta: {
- keepAlive: true // 需要缓存
- }
- }, {
- path: 'table',
- name: 'table',
- component: () => import('../views/Table/Table.vue'),
- meta: {
- keepAlive: false // 不需要缓存
- }
- }, {
- path: 'echarts',
- name: 'echarts',
- component: () => import('../views/Charts/Charts.vue'),
- meta: {
- keepAlive: false // 不需要缓存
- }
- }, {
- path: 'SysAdmin',
- name: 'SysAdmin',
- component: () => import('../views/SysAdmin/SysAdmin.vue'),
- meta: {
- keepAlive: false // 不需要缓存
- }
- }, {
- path: '',
- name: 'map',
- component: () => import('../views/Map3d/Map3DMain.vue'),
- meta: {
- keepAlive: true // 需要缓存
- }
- }
- ]
- }, {
- path: '/login',
- component: () => import('/src/views/Login/Login.vue')
- }, {
- path: '/',
- component: () => import('/src/views/Login/Login.vue')
- }
- ];
- // 错误路由
- const errorRoute = {
- path: '/:pathMatch(.*)',
- redirect: '/404'
- }
- const router = createRouter({
- history: createWebHashHistory(),
- routes: constantRoutes
- })
- // 白名单列表
- const whiteList = ['/login']
- // 路由加载前(全局路由前置守卫)
- router.beforeEach((to, from, next) => {
- console.log('store', store.token)
- NProgress.start();
- // token存在的情况
- if (store.token || to.path=="/login") {
- next();
- NProgress.done();
- } else {
- // 没有token
- next('/login') // 否则全部重定向到登录页
- NProgress.done();
- }
- });
- // 路由加载后(全局路由后置守卫)
- router.afterEach(() => {
- NProgress.done();
- });
- // 获取扁平化路由,将多级路由转换成一级路由
- // export const getKeepAliveRoutes = (rs, breadcrumb) => {
- // const routerList = []
- // rs.forEach((item) => {
- // if (item.meta.title) {
- // // breadcrumb.push(item.meta.title)
- // }
- // if (item.children && item.children.length > 0) {
- // routerList.push(...getKeepAliveRoutes(item.children, breadcrumb))
- // } else {
- // // item.meta.breadcrumb.push(...breadcrumb)
- // routerList.push(item)
- // }
- // // breadcrumb.pop()
- // })
- // return routerList
- // }
- // 加载vue组件
- // const layoutModules =
- // import.meta.glob('/src/views/**/*.vue')
- // 根据路径,动态获取vue组件
- // const getDynamicComponent = (path) => {
- // const component = layoutModules[`/src/views/${path}.vue`]
- // if (!component) {
- // console.error('组件不存在,路径为:', path)
- // }
- // return component
- // }
- // 根据菜单列表,生成路由数据
- // export const generateRoutes = (menuList) => {
- // const routerList = []
- // menuList.forEach((menu) => {
- // let component;
- // let path;
- // if (menu.children && menu.children.length > 0) {
- // component = () => import('@/layouts/backend/index.vue')
- // path = '/p/' + menu.menuId
- // } else {
- // component = getDynamicComponent(menu.component)
- // path = menu.menuUrl
- // }
- // const route = {
- // path: path,
- // name: menu.menuName,
- // component: component,
- // children: [],
- // meta: {
- // title: menu.menuName,
- // icon: menu.menuIcon,
- // id: '' + menu.menuId,
- // cache: true,
- // _blank: menu.openStyle === 1,
- // breadcrumb: []
- // }
- // }
- // // 有子菜单的情况
- // if (menu.children && menu.children.length > 0) {
- // route.children?.push(...generateRoutes(menu.children))
- // }
- // routerList.push(route)
- // })
- // return routerList
- // }
- export default router;
|