You can register async dynamic components locally in a single file component like this:
export default {
components: {
'test-dynamic': () => import('@/components/testDynamic'),
'other-dynamic': () => import('@/components/otherDynamic')
},
data () {
return {
current: 'test-dynamic'
}
}
}
And in your template:
<component :is="current"></component>
If you register multiple components then you would just change the value of current
to the desired component.
In the case of many components, you can import an object mapping the component names to their respective file paths, then register them like:
import myComponents from '@/components'
export default {
components: Object.keys(myComponents).reduce((obj, name) => {
return Object.assign(obj, { [name]: () => import(myComponents[name]) })
}, {})
...
}
Where myComponents
is exported as:
// components/index.js
export default {
foo: '@/path/to/Foo',
bar: '@/path/to/Bar',
...
}