feat: router修改为自动导入 (#223)

* feat: router修改为自动导入

* fix: 修复CI编译错误并移除多余声明

* feat: 添加orderNo令自动导入的路由可以排序
This commit is contained in:
悠静萝莉 2022-07-12 14:14:44 +08:00 committed by GitHub
parent 3afb38d1d1
commit 4067e7ca30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 11 deletions

View File

@ -7,6 +7,10 @@ const getMenuList = (list: MenuRoute[], basePath?: string): MenuRoute[] => {
if (!list) {
return [];
}
// 如果meta中有orderNo则按照从小到大排序
list.sort((a, b) => {
return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
});
return list
.map((item) => {
const path = basePath ? `${basePath}/${item.path}` : item.path;

View File

@ -1,13 +1,21 @@
import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import baseRouters from './modules/base';
import componentsRouters from './modules/components';
import othersRouters from './modules/others';
// 自动导入modules文件夹下所有ts文件
const modules = import.meta.globEager('./modules/**/*.ts');
// 路由暂存
const routeModuleList: Array<RouteRecordRaw> = [];
Object.keys(modules).forEach((key) => {
const mod = modules[key].default || {};
const modList = Array.isArray(mod) ? [...mod] : [mod];
routeModuleList.push(...modList);
});
// 关于单层路由meta 中设置 { single: true } 即可为单层路由,{ hidden: true } 即可在侧边栏隐藏该路由
// 存放动态路由
export const asyncRouterList: Array<RouteRecordRaw> = [...baseRouters, ...componentsRouters, ...othersRouters];
export const asyncRouterList: Array<RouteRecordRaw> = [...routeModuleList];
// 存放固定的路由
const defaultRouterList: Array<RouteRecordRaw> = [

View File

@ -10,13 +10,6 @@ declare module '*.vue' {
declare type ClassName = { [className: string]: any } | ClassName[] | string;
declare interface ImportMeta {
env: {
MODE: 'mock' | 'development' | 'test' | 'release';
};
glob: (url: string) => { url };
}
declare module '*.svg' {
const CONTENT: string;
export default CONTENT;

View File

@ -7,7 +7,10 @@
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"lib": ["esnext", "dom"],
"types": ["vite/client"],
"noEmit": true,
"baseUrl": "./",
"paths": {