From af54fb26b6e51580d30c77d9db01b0e38d9eb1c2 Mon Sep 17 00:00:00 2001 From: cshaptx4869 <994774638@qq.com> Date: Thu, 30 May 2024 14:16:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=B3=A8=E5=86=8Cvue=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lang/index.ts | 6 ++++++ src/main.ts | 18 +++--------------- src/plugins/i18n.ts | 7 ------- src/plugins/index.ts | 27 ++++++++++++++++++++++++--- src/router/index.ts | 6 ++++++ 5 files changed, 39 insertions(+), 25 deletions(-) delete mode 100644 src/plugins/i18n.ts diff --git a/src/lang/index.ts b/src/lang/index.ts index dad43956..ec3e6ccc 100644 --- a/src/lang/index.ts +++ b/src/lang/index.ts @@ -1,3 +1,4 @@ +import type { App } from "vue"; import { createI18n } from "vue-i18n"; import { useAppStoreHook } from "@/store/modules/app"; // 本地语言包 @@ -22,4 +23,9 @@ const i18n = createI18n({ globalInjection: true, }); +// 全局注册 i18n +export function setupI18n(app: App) { + app.use(i18n); +} + export default i18n; diff --git a/src/main.ts b/src/main.ts index 405fcb33..16e0cb9f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,6 @@ import { createApp } from "vue"; import App from "./App.vue"; -import router from "@/router"; -import { setupStore } from "@/store"; -import { setupDirective } from "@/directive"; -import { setupElIcons, setupI18n, setupPermission } from "@/plugins"; +import setupPlugins from "@/plugins"; // 本地SVG图标 import "virtual:svg-icons-register"; @@ -15,14 +12,5 @@ import "uno.css"; import "animate.css"; const app = createApp(App); -// 全局注册 自定义指令(directive) -setupDirective(app); -// 全局注册 状态管理(store) -setupStore(app); -// 全局注册Element-plus图标 -setupElIcons(app); -// 国际化 -setupI18n(app); -// 注册动态路由 -setupPermission(); -app.use(router).mount("#app"); +app.use(setupPlugins); +app.mount("#app"); diff --git a/src/plugins/i18n.ts b/src/plugins/i18n.ts deleted file mode 100644 index 16915cc1..00000000 --- a/src/plugins/i18n.ts +++ /dev/null @@ -1,7 +0,0 @@ -// 国际化 -import i18n from "@/lang/index"; -import type { App } from "vue"; - -export function setupI18n(app: App) { - app.use(i18n); -} diff --git a/src/plugins/index.ts b/src/plugins/index.ts index b54ee18a..65262c85 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -1,3 +1,24 @@ -export * from "./icons"; -export * from "./i18n"; -export * from "./permission"; +import { setupDirective } from "@/directive"; +import { setupI18n } from "@/lang"; +import { setupRouter } from "@/router"; +import { setupStore } from "@/store"; +import type { App } from "vue"; +import { setupElIcons } from "./icons"; +import { setupPermission } from "./permission"; + +export default { + install(app: App) { + // 自定义指令(directive) + setupDirective(app); + // 路由(router) + setupRouter(app); + // 状态管理(store) + setupStore(app); + // 国际化 + setupI18n(app); + // Element-plus图标 + setupElIcons(app); + // 路由守卫 + setupPermission(); + }, +}; diff --git a/src/router/index.ts b/src/router/index.ts index b67fdf5f..e1d92422 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,3 +1,4 @@ +import type { App } from "vue"; import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"; export const Layout = () => import("@/layout/index.vue"); @@ -117,6 +118,11 @@ const router = createRouter({ scrollBehavior: () => ({ left: 0, top: 0 }), }); +// 全局注册 router +export function setupRouter(app: App) { + app.use(router); +} + /** * 重置路由 */