Skip to content

Instantly share code, notes, and snippets.

@mikemand
Created July 1, 2022 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemand/9143e5eec577759f137d3211c5de7985 to your computer and use it in GitHub Desktop.
Save mikemand/9143e5eec577759f137d3211c5de7985 to your computer and use it in GitHub Desktop.
Old-school dynamic/async Vue components
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
*/
// Register all Async Vue components
for (const file of require.context('./', true, /\.vue$/i, 'lazy').keys()) {
const componentName = file
.split('/')
.splice(2) // Remove folder name, e.g. 'components' or 'views', from filenames.
.map((s) => s.charAt(0).toUpperCase() + s.slice(1)) // Make sure component names are capitalized.
.join('')
.replace('.vue', '') // Remove .vue extension from filenames.
;
if (componentName !== '') {
Vue.component(componentName, () => import(`${file}` /* webpackChunkName: "backend/[request]" */));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment