Why did I get blank (empty) content when I used async setup() in Vue.js 3?

13,087

Solution 1

Your component's async setup() looks fine other than the missing await res.json(), which still wouldn't cause the problem you're seeing. I suspect your usage of <Suspense> is incorrect.

To use async setup() in a component, the parent component must use that component in a <Suspense> tag:

<!-- Parent.vue -->
<template>
 <Suspense>
   <MyAsyncComponent />
 </Suspense>
</template>

You could also use the default and fallback slots of <Suspense> to show a loading indicator while waiting for the child component's setup to resolve:

<!-- Parent.vue -->
<template>
 <Suspense>
   <template #default>
     <MyAsyncComponent />
   </template>
   <template #fallback>
     <span>Loading...</span>
   </template>
 </Suspense>
</template>

demo

Solution 2

File parent.vue

<template>
  <!-- parent add <suspense> -->
  <suspense>
    <child />
  </suspense>
</template>

<script>
import Child from './child.vue'

export default {
  components: {
    child
  },
  setup() {
    return {}
  }
}
</script>

File child.vue

<template>
  <div>child</div>
</template>

<script>
export default {
  async setup() {
    return {}
  }
}
</script>

For child.vue, use async setup.... For parent.vue, you need to add <suspense> to child.vue.

Share:
13,087
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    Problem

    I use async setup() in Vue.js 3, but I got my HTML content to disappear. My component template did not insert to HTML, but when I remove the async and await prefix, my HTML content comes back. How can I fix this?

    async setup () {
        const data = ref(null)
        try {
            const res = await fetch('api')
            data.value = res.json()
        }
        catch (e) {
            console.error(e)
        }
        return {
            data
        }
    }
    

    I've tried

    1. I checked fetch, and it returned the correct response
    2. I've tried <Suspense> tag, but still the same problem
  • John Willemse
    John Willemse over 2 years
    I've wasted hours on this exact problem, your answer is a lifesaver, so thank you!!
  • Beaumont
    Beaumont over 2 years
    Just want to say this answer is GOLD. Thank you.
  • Hamza Dahmoun
    Hamza Dahmoun about 2 years
    Thank you. It took me 4 hours of debugging already.