To use the composable pattern in Vue.js version 3 with <script setup lang="ts">
, you can define a composable function that returns an object with reactive properties for loading
, error
, and data
.
Then, to wait for loading to finish on the first composable before running the second one, you can use await
and an isFinishedLoading
variable to keep track of the loading state.
Here's an example:
index.ts1402 chars60 lines
In this example, the first composable function useFirstComp
defines reactive properties for loading
, error
, and data
. It uses the fetch
API to make a request to an API endpoint and updates the reactive properties accordingly.
The second composable function useSecondComp
takes firstComp
as an argument and defines the same reactive properties. It sets loading
to true and awaits firstComp.loading
to wait for the first composable to finish loading.
Once loading is finished on the first composable, isFinishedLoading
is set to true
and the second composable continues with its logic, making a request to another API endpoint and updating its reactive properties.
Finally, in the template section, the reactive properties from both composables are displayed conditionally depending on their state.
gistlibby LogSnag