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); }