diff --git a/src/utils/common.ts b/src/utils/common.ts index 497a9b9a..8e98b642 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -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( + modules: Record Promise>, + 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'; diff --git a/src/views/workflow/process-instance/index.vue b/src/views/workflow/process-instance/index.vue index d887128d..571a1060 100644 --- a/src/views/workflow/process-instance/index.vue +++ b/src/views/workflow/process-instance/index.vue @@ -1,6 +1,5 @@