+14
-14
| import { App, inject } from "vue"; | ||
| import { IMenuRoutes, LayoutConfig } from "./type"; // 假设type.ts和hooks文件在同一目录 | ||
| let ACCESS_KEY: symbol | ''; | ||
| let LAYOUT_CONFIG: symbol | ''; | ||
| let MENU_ROUTES: symbol | ''; | ||
| export const useAccess = () => { | ||
| return (inject(ACCESS_KEY, '') as ReturnType< | ||
| (typeof import("src/access"))["default"] | ||
| >) | ||
| export const useAccess = () => { | ||
| ACCESS_KEY = inject('@@access-key',''); | ||
| return ACCESS_KEY | ||
| ? (inject(ACCESS_KEY) as ReturnType< | ||
| (typeof import("src/access"))["default"] | ||
| >) | ||
| : {}; | ||
| }; | ||
| export const useMenuRoutes = () => { | ||
| MENU_ROUTES = inject('@@routes-key',''); | ||
| return MENU_ROUTES ? (inject(MENU_ROUTES) as IMenuRoutes) : {}; | ||
| return inject(MENU_ROUTES,[]) as IMenuRoutes[] | ||
| }; | ||
| export const useLayout = () => { | ||
| LAYOUT_CONFIG = inject('@@layout-key','') as ''; | ||
| return LAYOUT_CONFIG ? (inject(LAYOUT_CONFIG) as LayoutConfig) : {} | ||
| return inject(LAYOUT_CONFIG,'') as LayoutConfig | ||
| } | ||
| export const useStore = <T = any>(namespace: string): T => { | ||
| const modules = inject(STORE_KEY,'') | ||
| if (!modules?.[namespace]) { | ||
| throw new Error(`[useStore] 未找到命名空间为 ${namespace} 的模块`) | ||
| } | ||
| return modules[namespace]() as T | ||
| } |
+2
-3
| { | ||
| "name": "mooljs", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "description": "function tool library for mooljs", | ||
@@ -37,4 +37,3 @@ "main": "lib/index.ts", | ||
| "@types/qs": "^6.9.15", | ||
| "store": "^2.0.12", | ||
| "@vueuse/core": "^13.0.0" | ||
| "store": "^2.0.12" | ||
@@ -41,0 +40,0 @@ }, |
| import {createGlobalState} from '@vueuse/core'; | ||
| const modules = {} as Record<string, () => any>; | ||
| /** | ||
| * 加载store下的所有命名空间模块,不支持嵌套定义 | ||
| * @param {string} _namespace 命名空间 | ||
| * @param {Record<string,any>} initailState 初始状态 | ||
| */ | ||
| export const createStore = async (_namespace,initailState:Record<string,any>) => { | ||
| // 自动导入所有模块 | ||
| const moduleFiles = import.meta.glob<{ default: () => any }>( | ||
| "/src/store/*.ts", | ||
| { eager: true }, | ||
| ); | ||
| for (const path in moduleFiles) { | ||
| const moduleName = path.match(/([^/]+)\.ts$/)?.[1]; | ||
| if (moduleName) { | ||
| modules[moduleName] = createGlobalState(moduleFiles[path].default); | ||
| } | ||
| } | ||
| if(_namespace){ | ||
| modules[_namespace] = createGlobalState(()=>initailState); | ||
| } | ||
| return modules; | ||
| } | ||
| /** | ||
| * @param {string} _namespace {} | ||
| */ | ||
| export const useStore = (_namespace: string) => { | ||
| console.log(modules); | ||
| return modules[_namespace]?.()||{}; | ||
| } |
5
-16.67%39728
-2.62%16
-5.88%1323
-2.43%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed