vuejs / rfcs

RFCs for substantial changes / feature additions to Vue core
4.87k stars 548 forks source link

Stop script setup from executing any further #574

Closed bediu closed 12 months ago

bediu commented 1 year ago

What problem does this feature solve?

I want to use "return" or (or something different) to stop a script setup code from running.

In the example, the functions should only run if the dynamic list is bigger than 0.

I understand I can invert the ifs, but imagine if we didn't import these functions, but instead, you had to write the code directly. Suddenly you need your whole component's code wrapped around an if when all you can do is prevent the code from running under a specific condition.

What does the proposed API look like?

<script lang="ts" setup>

    import dynamicList from 'somewhere';
    import { apiRequest, dispatchReportsData, clearUserData, clearCompanyData } from 'somewhere-else';

    if (dynamicList.length > 0) {
        return; // <-- stop script setup
    }

    apiRequest();
    dispatchReportsData();
    clearUserData();
    clearCompanyData();

</script>
sxzz commented 1 year ago

Consider the example:

<script setup>
import { ref } from 'vue'

if (false)
  return

const msg = ref('Hello World!')
</script>

<template>
  <h1>{{ msg }}</h1>
  <input v-model="msg">
</template>

The msg variable is declared after the return statement, so the template can't access it since it is never defined.