Is it possible to have an early return in the Vue setup script?
<template>
<div v-if="error || !data">Fallback content...</div>
<div v-else>Page content...</div>
</template>
<script setup lang="ts">
// ...
const { data, error } = await fetchApi()
// Early return here? E.g., if (error || !data) return
// ...
// Lots of code which doesn't need to run if error.
// E.g., calculation of variables only used in the page content.
// ...
</script>
Note: I'm coming from the React world and kinda new with Vue.
you can throw an error:
const {data, error} = await fetchApi()
if(error || !data) throw new Error('your error message')
It prevent the rest of the code to run
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With