Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load all Vue components from a given folder, no need for an "index.js"-file
const req = require.context('./components/', true, /\.(js|vue)$/i);
req.keys().map(key => {
const name = key.match(/\w+/)[0];
return Vue.component(name, req(key))
});
@sagalbot

This comment has been minimized.

sagalbot commented Oct 21, 2017

Nice, thanks!

@MartinMuzatko

This comment has been minimized.

MartinMuzatko commented Aug 9, 2018

With this, I get the following error:

[Vue warn]: Failed to mount component: template or render function not defined.
@maheshshah196

This comment has been minimized.

maheshshah196 commented Dec 1, 2018

Whenever importing vue component using require statement instead of import use 'default' key, see below code

const req = require.context('./components/', true, /\.(js|vue)$/i); req.keys().map(key => { const name = key.match(/\w+/)[0]; return Vue.component(name, req(key).default) });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment