I’m quite new with Vue, and I’m trying to lazy load a Component, I’m getting this error which I don’t understand, there should probably be a syntax error. Here’s the code:
JavaScript
x
28
28
1
<template>
2
<div class="container">
3
<h1>Async testing</h1>
4
<button @click="showModal=!showModal">show modal</button>
5
<Modal v-if=showModal />
6
</div>
7
8
</template>
9
10
<script>
11
12
import { defineAsyncComponent, ref } from 'vue';
13
export default {
14
components: {
15
Modal: () => defineAsyncComponent(
16
() => import('@/components/Modal.vue')
17
)
18
},
19
setup(){
20
const showModal = ref(false);
21
return {
22
showModal
23
}
24
}
25
}
26
27
</script>
28
The Modal Comp is just a simple h2 and a p with a red border (I already tried the same code without lazy load, and it works fine).
Here’s the error:
Advertisement
Answer
By wrapping defineAsyncComponent()
in a function, you’re incorrectly declaring the Modal
component as a function:
JavaScript
1
7
1
export default {
2
components: {
3
/* 👇 don't do this */
4
Modal: () => defineAsyncComponent(() => import('@/components/Modal.vue'))
5
},
6
}
7
The simplest solution is to remove the function wrapper:
JavaScript
1
6
1
export default {
2
components: {
3
Modal: defineAsyncComponent(() => import('@/components/Modal.vue'))
4
},
5
}
6