[Vuejs]-How to import components to add them to routes with Vue and VueRouter

0👍

Chris Fritz had a talk where explain it

His presentation is in github

I don’t understand so much this code, but it works

import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'

// Require in a base component context
const requireComponent = require.context(
    './components', false, /base-[\w-]+\.vue$/
);

requireComponent.keys().forEach(fileName => {
    // Get component config
    const componentConfig = requireComponent(fileName)

    // Get PascalCase name of component
    const componentName = upperFirst(
        camelCase(filename.replace(/^\.\//,'').replace(/\.\w+$/, ''))
    )

    // Register component globally
    Vue.component(componentName, componentConfig.default || componentConfig)
})

Leave a comment