From 66499d06e91076c0d4a5ea0c37105547371ddde1 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Thu, 23 Mar 2023 07:42:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=A8=E5=B1=80=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8C=87=E4=BB=A4=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 40c40fa90d88d9efad4ef2fb73b00d650427fd05 --- src/directive/index.ts | 10 +++++++++- src/main.ts | 16 +++++++--------- src/store/index.ts | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/directive/index.ts b/src/directive/index.ts index 39edd276..960fa44f 100644 --- a/src/directive/index.ts +++ b/src/directive/index.ts @@ -1 +1,9 @@ -export { hasPerm, hasRole } from './permission'; +import type { App } from 'vue'; + +import { hasPerm } from './permission'; + +// 全局注册 directive +export function setupDirective(app: App) { + // 使 v-hasPerm 在所有组件中都可用 + app.directive('hasPerm', hasPerm); +} diff --git a/src/main.ts b/src/main.ts index 920bc988..9a9fe032 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,8 @@ -import { createApp, Directive } from 'vue'; +import { createApp } from 'vue'; import App from './App.vue'; import router from '@/router'; import { setupStore } from '@/store'; +import { setupDirective } from '@/directive'; import '@/permission'; @@ -11,18 +12,15 @@ import 'virtual:svg-icons-register'; // 国际化 import i18n from '@/lang/index'; +// 样式 import '@/styles/index.scss'; import 'element-plus/theme-chalk/dark/css-vars.css'; - import 'uno.css'; const app = createApp(App); -// 自定义指令 -import * as directive from '@/directive'; -Object.keys(directive).forEach(key => { - app.directive(key, (directive as { [key: string]: Directive })[key]); -}); - -// 全局挂载 +// 全局注册 自定义指令(directive) +setupDirective(app); +// 全局注册 状态管理(store) setupStore(app); + app.use(router).use(i18n).mount('#app'); diff --git a/src/store/index.ts b/src/store/index.ts index fc0ba49d..ceb4219d 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -3,7 +3,7 @@ import { createPinia } from 'pinia'; const store = createPinia(); -// 全局挂载store +// 全局注册 store export function setupStore(app: App) { app.use(store); }