feat: add app fof iife

This commit is contained in:
2024-12-22 14:49:48 +08:00
parent 3b19cd8581
commit 9b3de5eba3
4 changed files with 34 additions and 1 deletions

12
src/app.ts Normal file
View File

@@ -0,0 +1,12 @@
// 当前的功能把所有的模块注入到windows对象当中
import { useConfigKey, useContextKey } from './web-env.ts';
import { QueryRouterServer } from '@kevisual/router/browser';
// bind to window, 必须要的获取全局的环境变量
window.useConfigKey = useConfigKey;
window.useContextKey = useContextKey;
window.QueryRouterServer = QueryRouterServer;
// bind to window, 获取路由对象
useContextKey('app', () => new QueryRouterServer());

View File

@@ -4,6 +4,7 @@ type GlobalEnv = {
name?: string;
[key: string]: any;
};
// 从window对象中获取全局的环境变量如果没有则初始化一个
export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
const env: GlobalEnv = (window as any)[initKey];
const _env = env || initEnv;
@@ -17,6 +18,7 @@ export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
return window[initKey] as GlobalEnv;
};
// 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象
export const useEnvKey = <T = any>(key: string, init?: () => T | null, initKey = 'config'): T => {
const _env = useEnv({}, initKey);
if (key && _env[key]) {