From ea4098172a5365bf4e25530f1818c062ebb56529 Mon Sep 17 00:00:00 2001 From: jiayun zhang Date: Sun, 2 Oct 2022 16:23:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A0=E4=B8=8A=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=B7=AF=E7=94=B1=E8=A7=86=E5=9B=BE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/enums/eventEnmu.ts | 1 + src/event/index.ts | 2 ++ src/utils/permission.ts | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/enums/eventEnmu.ts b/src/enums/eventEnmu.ts index 88a1875f..b28e975e 100644 --- a/src/enums/eventEnmu.ts +++ b/src/enums/eventEnmu.ts @@ -9,5 +9,6 @@ const enum EventEnum { AFTER_LOGOUT = 'aftreLogout', // 退出成功后 BEFORE_LOCAL_CHANGE = 'beforeLocalChange', // 全局语言变更前 RESIZE = 'resize', // window变化时触发 + INIT_DYNAMIC_VIEWS_MODULES = 'initDynamicViewsModules', //初始化动态路由页面视图 } export default EventEnum; diff --git a/src/event/index.ts b/src/event/index.ts index 06310f21..f17618c6 100644 --- a/src/event/index.ts +++ b/src/event/index.ts @@ -6,6 +6,7 @@ import { NavigationFailure, RouteLocationNormalized } from 'vue-router'; import EventEnum from '@/enums/eventEnmu'; import { App } from 'vue'; import { Composer } from 'vue-i18n'; +import { DynamicViewsModules } from '@/utils/permission'; interface Events { // 事件总线 [EventEnum.START]: App; @@ -23,6 +24,7 @@ interface Events { locale: string; i18n: Composer; }; // 全局语言变更前 + [EventEnum.INIT_DYNAMIC_VIEWS_MODULES]: DynamicViewsModules; } const mitter = Mitt>(); // inferred as Emitter export { EventEnum as event, mitter }; diff --git a/src/utils/permission.ts b/src/utils/permission.ts index f70e52e7..021b5bc5 100644 --- a/src/utils/permission.ts +++ b/src/utils/permission.ts @@ -5,6 +5,8 @@ import { useUserStore } from '@/store'; import { RouteRecordRaw } from 'vue-router'; import { DefineComponent } from 'vue'; import log from './log'; +import { mitter } from '@/event'; +import EventEnum from '@/enums/eventEnmu'; /** * 用户是否具有权限 @@ -21,9 +23,13 @@ export function permission(rules?: string | string[]) { return useUserStore().rules!.some((rule) => rule === '*' || rules!.includes(rule)); } -let dynamicViewsModules: Record Promise>; +export type DynamicViewsModules = Record Promise>; +let dynamicViewsModules: DynamicViewsModules; export function initDynamicViewsModules() { - dynamicViewsModules = dynamicViewsModules || import.meta.glob('../views/**/*.{vue,tsx}'); + if (!dynamicViewsModules) { + dynamicViewsModules = import.meta.glob('../views/**/*.{vue,tsx}') as Record Promise>; + mitter.emit(EventEnum.INIT_DYNAMIC_VIEWS_MODULES, dynamicViewsModules); + } } //动态转换组件 export function transitionComponent(component: string) {