·
1 min read
Nuxt Tip: Lazy Load Components in Nuxt 3
MH
Michael Hoffmann
@mokkapps
You can easily dynamically import (also known as lazy-loading) a component in Nuxt 3 by adding the Lazy
prefix to the component name:
app.vue
<template>
<div>
<LazyHelloWorld />
</div>
</template>
Lazy loading can help to improve your JavaScript bundle size and thus your application's performance by only loading components when they are needed.
app.vue
<script setup lang="ts">
const show = ref(false)
</script>
<template>
<div>
<LazyHelloWorld v-if="show" />
<button v-if="!show" @click="show = true">Click to lazy load HelloWorld component</button>
</div>
</template>
You can use the Network tab in your browser's developer tools to verify that HelloWorld
is only loaded after the button has been clicked:
Try it yourself in this interactive StackBlitz project:
If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter :