1. 目录结构
components
2.普通注册-index.js
import Skeleton from './skeleton.vue'
import Carousel from './carousel'
import More from './more'
export default {
install (app) {
app.component(Skeleton.name, Skeleton)
app.component(Carousel.name, Carousel)
app.component(More.name, More)
}
}
这样的坏处是如果有越来越多的全局组件,需要一个一个单独的引入,会很麻烦
3.全局注册-index.js
export default {
install (app) {
const ctx = require.context('./', false, /\.vue$/)
ctx.keys().forEach(item => {
const component = ctx(item).default
app.component(component.name, component)
})
}
}