search

Home  >  Q&A  >  body text

Vue setup script returns early?

<p>Is it possible to return early in a Vue setup script? </p> <pre class="brush:html;toolbar:false;"><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> </pre> <p>Note: I come from the React world and am fairly new to Vue. </p>
P粉497463473P粉497463473452 days ago526

reply all(1)I'll reply

  • P粉458913655

    P粉4589136552023-08-30 14:29:22

    You can throw an error:

    const {data, error} = await fetchApi()
    if(error || !data) throw new Error('your error message')

    It prevents the rest of the code from running

    reply
    0
  • Cancelreply