| | |
| | | class ComponentLoader { |
| | | constructor() { |
| | | this.modules = import.meta.glob('../../views/**/*.vue') |
| | | this.warmPromises = new Map() |
| | | } |
| | | resolveModule(componentPath) { |
| | | if (!componentPath) { |
| | | return null |
| | | } |
| | | const fullPath = `../../views${componentPath}.vue` |
| | | const fullPathWithIndex = `../../views${componentPath}/index.vue` |
| | | return this.modules[fullPath] || this.modules[fullPathWithIndex] || null |
| | | } |
| | | /** |
| | | * 加载组件 |
| | |
| | | if (!componentPath) { |
| | | return this.createEmptyComponent() |
| | | } |
| | | const fullPath = `../../views${componentPath}.vue` |
| | | const fullPathWithIndex = `../../views${componentPath}/index.vue` |
| | | const module = this.modules[fullPath] || this.modules[fullPathWithIndex] |
| | | const module = this.resolveModule(componentPath) |
| | | if (!module) { |
| | | const fullPath = `../../views${componentPath}.vue` |
| | | const fullPathWithIndex = `../../views${componentPath}/index.vue` |
| | | console.error( |
| | | `[ComponentLoader] 未找到组件: ${componentPath},尝试过的路径: ${fullPath} 和 ${fullPathWithIndex}` |
| | | ) |
| | |
| | | return module |
| | | } |
| | | /** |
| | | * 预热组件模块,避免首次点击页面时再触发冷加载。 |
| | | */ |
| | | warm(componentPath) { |
| | | const normalizedPath = typeof componentPath === 'string' ? componentPath.trim() : '' |
| | | if (!normalizedPath) { |
| | | return Promise.resolve(false) |
| | | } |
| | | if (this.warmPromises.has(normalizedPath)) { |
| | | return this.warmPromises.get(normalizedPath) |
| | | } |
| | | const module = this.resolveModule(normalizedPath) |
| | | if (!module) { |
| | | return Promise.resolve(false) |
| | | } |
| | | const warmTask = module() |
| | | .then(() => true) |
| | | .catch((error) => { |
| | | console.warn(`[ComponentLoader] 组件预热失败: ${normalizedPath}`, error) |
| | | return false |
| | | }) |
| | | this.warmPromises.set(normalizedPath, warmTask) |
| | | return warmTask |
| | | } |
| | | /** |
| | | * 加载布局组件 |
| | | */ |
| | | loadLayout() { |