Answer the question
In order to leave comments, you need to log in
Require.context and lazy-loading?
There is a folder with files:
- first.vue
- second.vue
- third.vue
- index.js
index.js:
THIS IS A MAGIC
const context = require.context('./', false, /\.vue$/i)
export default context.keys().reduce((modules, path) => ({
...modules,
[/[a-z]+/i.exec(path).pop()]: context(path).default,
}), {})
import someComponents from '../pathWithComponents/index.js'
export default {
components: {
...someComponents
},
components: {
someComponent: () => import('./pathWithComponents/someComponent')
},
Answer the question
In order to leave comments, you need to log in
Here is the documentation
const requireComponents = {};
const requireComponentContext = require.context('./', false, /\.vue$/i, 'lazy'); //lazy!
requireComponentContext.keys().forEach((fileName, index) => {
//componentName может быть по названию файла, смотрите пример в документации.
const componentName = `MySuperComponent-${index + 1}`;
const componentConfig = requireComponentContext(fileName);
requireComponents[componentName] = () => componentConfig;
});
//vue instance
export default {
components: {
...requireComponents
}
}
<my-super-component-1></my-super-component-1>
<my-super-component-2></my-super-component-2>
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question