Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Early return in Vue setup script?

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.

like image 541
Michael Haar Avatar asked Aug 07 '26 21:08

Michael Haar


1 Answers

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

like image 185
Morteza Avatar answered Aug 09 '26 11:08

Morteza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!