Where to fetch data when using Composition API (Vue 3)? #9063
gamaralf
started this conversation in
General Discussions
Replies: 1 comment
-
<script lang="ts" setup>
async function myPost() {
const post = await fetch(`/api/post/1`).then((r) => r.json());
}
myPost(); // <- created
</script>
Top-level
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In Vue 2, it seemed to be consensus that the adequate place to put code that fetches external data was the "created" hook.
However, for Vue 3, I could not find a similar guideline. And it seems that there is no "created" hook
when using the Composition API to build a component. See https://vuejs.org/api/composition-api-lifecycle.html
So, where should I place code to fetch data from an HTTP source in a component using the Composition API? Directly within "<script setup>"? In a "onMounted" hook function?
I am talking about fetching data from the browser, no SSR involved here.
Beta Was this translation helpful? Give feedback.
All reactions