# Views
Similarly to the components convention, Vue-Autowire lets you provide 2 different require.context instances when defining the views convention:
Vue.use(VueAutowire, {
views: {
requireContext: require.context('@/views', true, /\/(?:[^.]+|(?!\.local\.vue$)|(?!\.async\.vue$))\.vue$/),
requireAsyncContext: require.context('@/views', true, /async\.vue$/, 'lazy')
}
})
- The first one,
requireContextlets you define the views that will be registered within Vue as regular sync components, to be included in the main webpack bundle. - The second one,
requireAsyncContextlets you define the views that will be registerd within Vue 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 views default convention
import defaultViewConventions from 'vue-autowire/src/conventions/views'
It is defined as follows:
{
views: {
requireContext: require.context('@/views', true, /\/(?:[^.]+|(?!\.local\.vue$)|(?!\.async\.vue$))\.vue$/),
requireAsyncContext: require.context('@/views', 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@/viewsfolder 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@/viewsfolder 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 view)
# Example
Given a folder structure like
/src
/views
home.vue
about.async.vue
/profile-settings
profile-settings.async.vue
security-settings.local.vue // manually imported from profile-settings.async.vue
/components
...
/directives
...
/mixins
...
App.vue
main.js
router.js
The default views convention will:
- Register
homeas Vue sync components, exactly with that names. - Include
home.vuein the main webpack bundle - Register
aboutandprofile-settingsas Vue async components, exactly with those names. - Include
about.async.vueinto its own webpack bundle - Include
profile-settings.async.vueandsecurity-settings.local.vueinto its own webpack bundle (assuming you manually importsecurity-settings.local.vuefromprofile-settings.async.vue)
# Router integration
Most likely, you will define views so they can be defined as the component of vue-router routes. Update your routes definition to use the components that Vue-Autowire will have registered:
{
path: '/',
name: 'home',
// Instead of manually importing here the component, use the one that Vue-Autowire registered
// The name is based on the file name
component: Vue.component('home')
},
TIP
This will work even if you defined the view as an async one!
Then you want to make sure that Vue-Autowire is able to find and register all the views before you construct the routes.
For example, make sure the file where you create the Router instance exports a function rather than the instance:
export default () => new Router({
routes: [
...
]
}
This way, in your main app.js file, you can execute the function to get the router only after Vue-Autowire has been registered:
Vue.use(VueAutowire, defaultConventions);
...
new Vue({
router: router(),
render: h => h(App)
}).$mount('#vue-holder');
← Components Routes →