OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Nuxt 3: How to use useHead() with async data fetched inside useAsyncData()

  • Thread starter Thread starter Robin Klöckner
  • Start date Start date
R

Robin Klöckner

Guest
In my component's <script setupt> I am fetching data from my CMS (Strapi v4) inside useAsyncData, since the given route is pre-rendered server-side. In a second step I want to use the fetched data inside useHead() but the the title is either undefined or the application breaks.

Code:
<script setup>
const { find } = useStrapi4();

const { data } = await useAsyncData(
    "posts",
    () => find("posts"),
);

useHead({
    title: data.data[0].attributes.title,
})
</script>

Is there away to achieve this, without integrating useHead() in useAsyncData()? What am I missing?

<p>In my component's <code><script setupt></code> I am fetching data from my CMS (Strapi v4) inside <code>useAsyncData</code>, since the given route is pre-rendered server-side. In a second step I want to use the fetched data inside <code>useHead()</code> but the the title is either undefined or the application breaks.</p>
<pre><code><script setup>
const { find } = useStrapi4();

const { data } = await useAsyncData(
"posts",
() => find("posts"),
);

useHead({
title: data.data[0].attributes.title,
})
</script>
</code></pre>
<p>Is there away to achieve this, without integrating <code>useHead()</code> in <code>useAsyncData()</code>? What am I missing?</p>
 

Latest posts

Top