Custom Loading Screen when the browser is still process rendering
How to make or custom loading screen when the browser is still process rendering in NuxtJS 3.
<NuxtLoadingIndicator> component:<NuxtLoadingIndicator> component to your app.vue file or layout:useLoadingIndicator composable:useLoadingIndicator composable:isLoading and progress useLoadingIndicator Composable (https://nuxt.com/docs/api/composables/use-loading-indicator).ClientOnly component to ensure it only renders on the client side:``
Remember that the ClientOnly` component is used for purposely rendering a component only on the client side ClientOnly Component (https://nuxt.com/docs/api/components/client-only).useLoadingIndicator composable in Nuxt 3, specifically that the isLoading state remains false even after refreshing and delaying the network. Let's look into this issue based on the available information.useLoadingIndicator composable is designed to work with Nuxt's built-in page navigation system. However, it seems that in some cases, it might not behave as expected. Here are a few things to consider:useLoadingIndicator, you can try hooking into Nuxt's page loading events directly. Here's an example:suspensible: false option in your defineAsyncComponent calls, as this can affect how loading components are displayed Stack Overflow - Loading component in defineAsyncComponent (https://stackoverflow.com/questions/78590292).useLazyFetch or useLazyAsyncData:pending state that you can use to show a loading indicator:<NuxtLoadingIndicator><NuxtLoadingIndicator>app.vueuseLoadingIndicatoruseLoadingIndicatoruseLoadingIndicatoruseLoadingIndicatoruseLoadingIndicatorisLoadingisLoadingprogressClientOnly``
Remember that the suspensible: falsedefineAsyncComponentuseLazyFetchuseLazyAsyncDatapending<template>
<NuxtLoadingIndicator />
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template><NuxtLoadingIndicator :height="5" color="#00dc82" /><script setup lang="ts">
const { isLoading, progress, start, finish } = useLoadingIndicator()
// You can manually control the loading state
start()
// ... do some work
finish()
</script>
<template>
<div v-if="isLoading">Loading: {{ progress }}%</div>
</template><template>
<div v-if="loading" class="fixed left-0 top-0 h-0.5 w-full z-50 bg-green-500" />
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
<script setup lang="ts">
const nuxtApp = useNuxtApp();
const loading = ref(false);
nuxtApp.hook("page:start", () => {
loading.value = true;
});
nuxtApp.hook("page:finish", () => {
loading.value = false;
});
</script><script setup lang="ts">
const { start, finish, isLoading } = useLoadingIndicator()
// Manually start the loading indicator
start()
// Simulate some asynchronous operation
setTimeout(() => {
// Finish the loading indicator
finish()
}, 2000)
</script>
<template>
<div v-if="isLoading">Loading...</div>
<NuxtPage />
</template><script setup lang="ts">
const { pending, data: posts } = useLazyFetch('/api/posts')
</script>
<template>
<div v-if="pending">Loading...</div>
<div v-else>
<!-- Display your posts here -->
</div>
</template>