如何在NUXT3中使用其他方法或功能中获取的API结果

发布于 2025-02-11 05:35:00 字数 779 浏览 1 评论 0原文

如何使用NUXT3中的API获取数据?

例如,我想从API的结果设置元数据,但它引发了一个未定义的错误。我无法访问pagedata usehead

<script setup>
import { ref } from "vue"
const { apiUrl } = useRuntimeConfig()
const route = useRoute()

const { data: pageData } = await useFetch(
  `${apiUrl}/misc/cmspage/61785b119b4eb50d8d625621`
)

useHead({
   title: this.pageData.seo.metatitle
})
</script>

这是API的结果。

{
  seo: {
    metatitle: "SomeTitle",
    metadesc: "SomeDescriptions",
    metakeywords: "SomeKeywords",
  },
  pagename: "XXXXX",
  faqs: [],
  topDestinations: [],
  pageId: "61785b119b4eb50d8d625621",
  id: "61785b119b4eb50d8d625621",
}

How to work with fetched data from an API in Nuxt3?

For example, I want to set metatags from the API's results, but it throws an error of undefined. I am unable to access pageData in useHead.

<script setup>
import { ref } from "vue"
const { apiUrl } = useRuntimeConfig()
const route = useRoute()

const { data: pageData } = await useFetch(
  `${apiUrl}/misc/cmspage/61785b119b4eb50d8d625621`
)

useHead({
   title: this.pageData.seo.metatitle
})
</script>

This is the result of the API.

{
  seo: {
    metatitle: "SomeTitle",
    metadesc: "SomeDescriptions",
    metakeywords: "SomeKeywords",
  },
  pagename: "XXXXX",
  faqs: [],
  topDestinations: [],
  pageId: "61785b119b4eb50d8d625621",
  id: "61785b119b4eb50d8d625621",
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-02-18 05:35:00

您应该使用pagedata.value.seo.metatitle打印参考文献的内容。

请参见更多详细信息: https> https> https:// vuejs。 org/guide/Essentials/exiveitive-fundamentals.html#vactive-variables-with-ref (切换到右上角的sidebar api api preference

You should use pageData.value.seo.metatitle to print the content of your ref.

More details are available here: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#reactive-variables-with-ref (switch to composition on the top right sidebar API preference)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文