仅限于webpack
const requireComponent = require.context('./library', true, /\.vue$/)
export default {
async install (Vue) {
requireComponent.keys().forEach((item) => {
// console.log(requireComponent[item], 77) // ['./bread.vue', './pubArt.vue', './qq/q.vue']
/* 获取的是 每一个组件暴露出来的对象 */
const defaultObj = requireComponent(item).default
Vue.component(defaultObj.name, defaultObj)
})
}
vite
const requireComponent = import.meta.globEager('./library/*.vue')
export default {
install (Vue) {
Object.keys(requireComponent).forEach(item => {
const defaultObj = requireComponent[item].default
Vue.component(defaultObj.name, defaultObj)
})
}
}