I’m trying to make a preloader as a v-if component on the page for 2 second and than toggle view to another block. But I can’t get hoe to write it correctly. Also do I need to add an endSettimeout method ?
<script setup>
let loading = ref(false)
const showModal = () => {
loading = true
const load = setTimeout(() => {
loading = true
modalVisible.value = true
}, 2000)
loading = false
}
</script>
<template>
<div>
<div class="preloader" v-if="loading == true"></div>
<div class="wrapper">
<Modal v-model="modalVisible" v-if="modalVisible" />
<div class="form__container" v-else>
</div>
</template>
2
Answers
You must also start loading – e.g. when the component is loaded (with onMounted)
In your
setTimeout
, you should setloading
tofalse
after the timeout, not before it and you need to initializemodalVisible
as a reactive variable usingref()
.