Skip to content

Commit

Permalink
feat: 加上注册动态路由视图模块监听
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayun zhang committed Oct 2, 2022
1 parent 53b16ab commit ea40981
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/enums/eventEnmu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 2 additions & 0 deletions src/event/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ interface Events {
locale: string;
i18n: Composer<unknown, unknown, unknown>;
}; // 全局语言变更前
[EventEnum.INIT_DYNAMIC_VIEWS_MODULES]: DynamicViewsModules;
}
const mitter = Mitt<Events & Omit<{ [key in EventEnum]: undefined }, keyof Events>>(); // inferred as Emitter<Events>
export { EventEnum as event, mitter };
10 changes: 8 additions & 2 deletions src/utils/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
* 用户是否具有权限
Expand All @@ -21,9 +23,13 @@ export function permission(rules?: string | string[]) {
return useUserStore().rules!.some((rule) => rule === '*' || rules!.includes(rule));
}

let dynamicViewsModules: Record<string, () => Promise<DefineComponent>>;
export type DynamicViewsModules = Record<string, () => Promise<DefineComponent>>;
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<string, () => Promise<DefineComponent>>;
mitter.emit(EventEnum.INIT_DYNAMIC_VIEWS_MODULES, dynamicViewsModules);
}
}
//动态转换组件
export function transitionComponent(component: string) {
Expand Down

0 comments on commit ea40981

Please sign in to comment.