mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
refactor(utils): 简化加载组件方法
This commit is contained in:
@ -79,28 +79,24 @@ export function humpToLine(str: string, line: string = '-') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 动态加载组件 */
|
/** 动态加载组件 */
|
||||||
export async function loadDynamicComponent<T extends object = any>(
|
export async function loadDynamicComponent(
|
||||||
modules: Record<string, () => Promise<any>>,
|
modules: Record<string, () => Promise<any>>,
|
||||||
formPath: string,
|
formPath: string,
|
||||||
options?: {
|
{ delay = 2000, timeout = 3000 } = {}
|
||||||
delay?: number;
|
|
||||||
timeout?: number;
|
|
||||||
}
|
|
||||||
) {
|
) {
|
||||||
const expectedPathSuffix = `${humpToLine(formPath)}.vue`;
|
const suffix = `${humpToLine(formPath)}.vue`;
|
||||||
|
const matched = Object.entries(modules).find(([path]) => path.endsWith(suffix));
|
||||||
|
|
||||||
const matchedKey = Object.keys(modules).find(path => path.endsWith(expectedPathSuffix));
|
if (!matched) {
|
||||||
|
window.$message?.error(`组件不存在: ${suffix}`);
|
||||||
if (!matchedKey) {
|
throw new Error(`组件不存在: ${suffix}`);
|
||||||
window.$message?.error('组件不存在');
|
|
||||||
throw new Error(`组件不存在: ${expectedPathSuffix}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return markRaw(
|
return markRaw(
|
||||||
defineAsyncComponent({
|
defineAsyncComponent({
|
||||||
loader: async () => (await modules[matchedKey]()) as T,
|
loader: matched[1],
|
||||||
delay: options?.delay ?? 200,
|
delay,
|
||||||
timeout: options?.timeout ?? 3000
|
timeout
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user