Created
July 1, 2022 19:23
-
-
Save mikemand/9143e5eec577759f137d3211c5de7985 to your computer and use it in GitHub Desktop.
Old-school dynamic/async Vue components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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