const comModules = import.meta.glob('./**/index.vue') const _abvuePageList = Object.entries(comModules).map(([k, v]) => { const path = k.replace('./', '').replace('/index.vue', '') || '/' const name = path.split('/').filter(Boolean).join('-') || 'index' const component = comModules[k.replace('page.ts', 'index.vue')] const meta = { title: 'named1', type: 'master' } return { path, name, component, meta } })const comModules = import.meta.glob('./**/index.vue') const _abvuePageList = Object.entries(comModules).map(([k, v]) => { const path = k.replace('./', '').replace('/index.vue', '') || '/' const name = path.split('/').filter(Boolean).join('-') || 'index' const component = comModules[k.replace('page.ts', 'index.vue')] const meta = { title: 'named1', type: 'master' } return { path, name, component, meta } }) 优化
时间: 2023-06-09 15:08:48 浏览: 99
vite vue3 import.meta.glob动态路由
这段代码中有重复的部分,可以进行优化:
const comModules = import.meta.glob('./**/index.vue')
const _abvuePageList = Object.entries(comModules).map(([k, v]) => {
const path = k.replace('./', '').replace('/index.vue', '') || '/'
const name = path.split('/').filter(Boolean).join('-') || 'index'
const component = comModules[k.replace('page.ts', 'index.vue')]
const meta = { title: 'named1', type: 'master' }
return { path, name, component, meta }
})
可以将重复的部分提取出来,减少代码量和冗余:
const comModules = import.meta.glob('./**/index.vue')
const getPage = (k, v) => {
const path = k.replace('./', '').replace('/index.vue', '') || '/'
const name = path.split('/').filter(Boolean).join('-') || 'index'
const component = comModules[k.replace('page.ts', 'index.vue')]
const meta = { title: 'named1', type: 'master' }
return { path, name, component, meta }
}
const _abvuePageList = Object.entries(comModules).map(getPage)
阅读全文