mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
optimize(projects): 动态加载组件方法抽取为公共函数
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { defineAsyncComponent, markRaw } from 'vue';
|
||||
import { AcceptType } from '@/enum/business';
|
||||
import { $t } from '@/locales';
|
||||
|
||||
/**
|
||||
* Transform record to option
|
||||
*
|
||||
@ -76,6 +78,33 @@ export function humpToLine(str: string, line: string = '-') {
|
||||
return temp;
|
||||
}
|
||||
|
||||
/** 动态加载组件 */
|
||||
export async function loadDynamicComponent<T extends object = any>(
|
||||
modules: Record<string, () => Promise<any>>,
|
||||
formPath: string,
|
||||
options?: {
|
||||
delay?: number;
|
||||
timeout?: number;
|
||||
}
|
||||
) {
|
||||
const expectedPathSuffix = `${humpToLine(formPath)}.vue`;
|
||||
|
||||
const matchedKey = Object.keys(modules).find(path => path.endsWith(expectedPathSuffix));
|
||||
|
||||
if (!matchedKey) {
|
||||
window.$message?.error('组件不存在');
|
||||
throw new Error(`组件不存在: ${expectedPathSuffix}`);
|
||||
}
|
||||
|
||||
return markRaw(
|
||||
defineAsyncComponent({
|
||||
loader: async () => (await modules[matchedKey]()) as T,
|
||||
delay: options?.delay ?? 200,
|
||||
timeout: options?.timeout ?? 3000
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/** 判断是否为空 */
|
||||
export function isNotNull(value: any) {
|
||||
return value !== undefined && value !== null && value !== '' && value !== 'undefined' && value !== 'null';
|
||||
|
Reference in New Issue
Block a user