const comModules = import.meta.glob('./**/index.vue') const _abvuePageList = Object.entries(comModules).map(([k, v]) => { const path: string = k.replace('./', '').replace('/index.vue', '') || '/' const name: string = path.split('/').filter(Boolean).join('-') || 'index' const component = v as AsyncComponentLoader<Component<any>> const meta = { title: 'named1', type: 'master' } return { path, name, component, meta } }) 优化一下
时间: 2023-06-09 08:08:41 浏览: 439
vite vue3 import.meta.glob动态路由
以上代码可以进行如下优化:
1. 使用箭头函数简化代码:
```
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 = v as AsyncComponentLoader<Component<any>>
const meta = { title: 'named1', type: 'master' }
return { path, name, component, meta }
})
```
2. 使用可选链操作符简化代码:
```
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 = v as AsyncComponentLoader<Component<any>>
const meta = { title: 'named1', type: 'master' }
return { path, name, component, meta }
})
```
3. 使用解构赋值简化代码:
```
const comModules = import.meta.glob('./**/index.vue')
const _abvuePageList = Object.entries(comModules).map(([key, value]) => {
const path = key?.replace('./', '')?.replace('/index.vue', '') || '/'
const name = path?.split('/')?.filter(Boolean)?.join('-') || 'index'
const component = value as AsyncComponentLoader<Component<any>>
const meta = { title: 'named1', type: 'master' }
return { path, name, component, meta }
})
```
以上是对这段代码的简单优化,具体还要根据实际情况进行调整。
阅读全文