refactor: 全局注册自定义指令方式优化
Former-commit-id: 40c40fa90d88d9efad4ef2fb73b00d650427fd05
This commit is contained in:
@@ -1 +1,9 @@
|
|||||||
export { hasPerm, hasRole } from './permission';
|
import type { App } from 'vue';
|
||||||
|
|
||||||
|
import { hasPerm } from './permission';
|
||||||
|
|
||||||
|
// 全局注册 directive
|
||||||
|
export function setupDirective(app: App<Element>) {
|
||||||
|
// 使 v-hasPerm 在所有组件中都可用
|
||||||
|
app.directive('hasPerm', hasPerm);
|
||||||
|
}
|
||||||
|
|||||||
16
src/main.ts
16
src/main.ts
@@ -1,7 +1,8 @@
|
|||||||
import { createApp, Directive } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import App from './App.vue';
|
import App from './App.vue';
|
||||||
import router from '@/router';
|
import router from '@/router';
|
||||||
import { setupStore } from '@/store';
|
import { setupStore } from '@/store';
|
||||||
|
import { setupDirective } from '@/directive';
|
||||||
|
|
||||||
import '@/permission';
|
import '@/permission';
|
||||||
|
|
||||||
@@ -11,18 +12,15 @@ import 'virtual:svg-icons-register';
|
|||||||
// 国际化
|
// 国际化
|
||||||
import i18n from '@/lang/index';
|
import i18n from '@/lang/index';
|
||||||
|
|
||||||
|
// 样式
|
||||||
import '@/styles/index.scss';
|
import '@/styles/index.scss';
|
||||||
import 'element-plus/theme-chalk/dark/css-vars.css';
|
import 'element-plus/theme-chalk/dark/css-vars.css';
|
||||||
|
|
||||||
import 'uno.css';
|
import 'uno.css';
|
||||||
|
|
||||||
const app = createApp(App);
|
const app = createApp(App);
|
||||||
// 自定义指令
|
// 全局注册 自定义指令(directive)
|
||||||
import * as directive from '@/directive';
|
setupDirective(app);
|
||||||
Object.keys(directive).forEach(key => {
|
// 全局注册 状态管理(store)
|
||||||
app.directive(key, (directive as { [key: string]: Directive })[key]);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 全局挂载
|
|
||||||
setupStore(app);
|
setupStore(app);
|
||||||
|
|
||||||
app.use(router).use(i18n).mount('#app');
|
app.use(router).use(i18n).mount('#app');
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { createPinia } from 'pinia';
|
|||||||
|
|
||||||
const store = createPinia();
|
const store = createPinia();
|
||||||
|
|
||||||
// 全局挂载store
|
// 全局注册 store
|
||||||
export function setupStore(app: App<Element>) {
|
export function setupStore(app: App<Element>) {
|
||||||
app.use(store);
|
app.use(store);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user