According this article I would like to import component to view dynamically to my Vue 3 app. The code of the view looks like:
JavaScript
x
28
28
1
<template>
2
<div class="page">
3
<latest-box v-if="showLatestBox" />
4
</div>
5
</template>
6
7
8
<script>
9
// @ is an alias to /src
10
// This works well.
11
//import LatestBox from '@/components/LatestBox.vue'
12
13
14
export default {
15
name: 'Page 1',
16
17
data() {
18
return {
19
showLatestBox: true,
20
}
21
},
22
23
components: {
24
LatestBox: () => import('@/components/LatestBox.vue') // This does not work
25
}
26
}
27
</script>
28
Code does not throw any errors but I dont see the component on the page. If I use first import style it works. Am I missing somethig?
Advertisement
Answer
You need to use defineAsyncComponent
in Vue 3 to lazy load components
JavaScript
1
6
1
import { defineAsyncComponent } from 'vue'
2
3
components: {
4
LatestBox: defineAsyncComponent(() => import('@/components/LatestBox.vue'))
5
}
6
https://v3-migration.vuejs.org/breaking-changes/async-components.html#overview