# Components
Similarly to the views convention, Vue-Autowire lets you provide 2 different require.context instances when defining the components convention:
Vue.use(VueAutowire, {
components: {
requireContext: require.context('@/components', true, /\/(?:[^.]+|(?!\.local\.vue$)|(?!\.async\.vue$))\.vue$/),
requireAsyncContext: require.context('@/components', true, /async\.vue$/, 'lazy')
}
})
- The first one,
requireContextlets you define the components that will be registered as regular sync components, to be included in the main webpack bundle. - The second one,
requireAsyncContextlets you define the components that will be registerd as async components, to be included on their own bundle by using the lazy mode of webpack's require.context API.
# Default Convention
The default convention will be used with either of these:
// Use all of the default conventions
import defaultConventions from 'vue-autowire/src/conventions'
// Use only the components default convention
import defaultComponentConventions from 'vue-autowire/src/conventions/components'
It is defined as follows:
{
components: {
requireContext: require.context('@/components', true, /\/(?:[^.]+|(?!\.local\.vue$)|(?!\.async\.vue$))\.vue$/),
requireAsyncContext: require.context('@/components', true, /async\.vue$/, 'lazy')
}
}
Which means:
- Assumes there is a webpack alias defined as
@which maps to the root source of the Vue application. If you are using the Vue-CLI, this is already the case - With
requireContext, all the files inside the@/componentsfolder that end with.vuebut not with.local.vuenor.async.vuewill be found. They will be registered as Vue components within the Vue application and included in the main bundle. - With
requireAsyncContext, all the files inside the@/componentsfolder that end withasync.vuewill be found. They will be registered as async Vue components within the Vue application and included in their own bundle. (One bundle per each async component)
# Example
Given a folder structure like
/src
/components
navbar.vue
footer.vue
markdown-editor.async.vue
/blog-entry-preview
blog-entry-preview.async.vue
author-preview.local.vue // manually imported from blog-entry-preview.async.vue
/views
...
/directives
...
/mixins
...
App.vue
main.js
The default components convention will:
- Register
navbarandfooteras Vue sync components, exactly with those names. - Include
navbar.vueandfooter.vuein the main webpack bundle - Register
markdown-editorandblog-entry-previewas Vue async components, exactly with those names. - Include
markdown-editor.vueinto its own webpack bundle - Include
blog-entry-preview.async.vueandauthor-preview.local.vueinto its own webpack bundle (assuming you manually importauthor-preview.local.vuefromblog-entry-preview.async.vue)
← Getting Started Views →