Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Register all base components
// --- Base components ---
import Vue from 'vue'
// To extract the component name
const nameReg = /([a-z0-9]+)\./i
function registerGlobalComponents (components) {
components.keys().forEach(key => {
const name = key.match(nameReg)[1]
Vue.component(name, {
name,
...components(key).default,
})
})
}
// Require all the components that start with 'BaseXXX.vue'
let components = require.context('@/common', true, /Base[a-z0-9]+\.(jsx?|vue)$/i)
registerGlobalComponents(components)
// Webpack HMR
if (module.hot) {
module.hot.accept(components.id, () => {
const components = require.context('@/common', true, /Base[a-z0-9]+\.(jsx?|vue)$/i)
registerGlobalComponents(components)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment