Nuxt 3 中有办法动态设置元标签吗?

huangapple go评论56阅读模式
英文:

Is there any way to set meta tags dynamically in nuxt 3?

问题

I'm trying to populate page title and meta tags with dynamic data coming from api for seo purpose. but unable to get it in view-page-source

I've tried useHead, useSeoMeta and useServerSeoMeta with reactive properties. but all these functions called before getting api response. is there any way to get dynamic meta data in view-source-page?

英文:

I'm trying to populate page title and meta tags with dynamic data coming from api for seo purpose. but unable to get it in view-page-source

I've tried useHead, useSeoMeta and useServerSeoMeta with reactive properties. but all these functions called before getting api response. is there any way to get dynamic meta data in view-source-page?

答案1

得分: 6

useSeoMeta对我来说有效。请注意,值必须是函数。

<script setup>

const id = route.params.id;
const { data: product } = await useAsyncData(id, () =>
  apiStore.GET_PRODUCT(id),
);

...

useSeoMeta({
  title: () => product.value?.name,
  description: () => product.value?.short_description,
  ogTitle: () => product.value?.name,
  ogDescription: () => product.value?.short_description,
  // and other stuff
});

<script>
英文:

useSeoMeta works for me. Note that values must be functions.

&lt;script setup&gt;

const id = route.params.id;
const { data: product } = await useAsyncData(id, () =&gt; 
  apiStore.GET_PRODUCT(id),
);

...

useSeoMeta({
  title: () =&gt; product.value?.name,
  description: () =&gt; product.value?.short_description,
  ogTitle: () =&gt; product.value?.name,
  ogDescription: () =&gt; product.value?.short_description,
  // and other stuff
});

&lt;script&gt;

huangapple
  • 本文由 发表于 2023年4月11日 15:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定