Skip to content

Instantly share code, notes, and snippets.

@xinglongjizi
Created February 14, 2025 06:47
Show Gist options
  • Save xinglongjizi/cb2a1ed59c0de66b7b31285a1dd7871e to your computer and use it in GitHub Desktop.
Save xinglongjizi/cb2a1ed59c0de66b7b31285a1dd7871e to your computer and use it in GitHub Desktop.
扫描当前文件夹中的所有vue组件,并保存到一个map中
/*
使用 Map 数据结构保存要渲染的卡片实例
{
'@/components/reportCenter/cards/MyComponent.vue': component的实例对象,import得到的东西
}
在视图中使用
<component :is="allCardComponentsMap.get(item.componentPath)" />
*/
import { defineAsyncComponent } from 'vue'
const allComponentsMap = new Map()
const allComponentsPath = require.context('./', true, /\.vue$/).keys()
allComponentsPath.forEach((path: string) => {
allComponentsMap.set(
'@/components/reportCenter/cards/' + path.slice(2),
defineAsyncComponent(() => import('@/components/reportCenter/cards/' + path.slice(2)))
)
})
export default allComponentsMap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment