[英]Nuxt: Dynamic head / meta title is undefined on ssr
I have a nuxt project, where the meta title and description comes (from nuxt/content).我有一个 nuxt 项目,其中元标题和描述来自(来自 nuxt/content)。 The async fetch for the data is made in index and received in a sub component via a getter.
数据的异步获取在索引中进行,并通过 getter 在子组件中接收。
On generate, the meta tags are there but on ssr not:/在生成时,元标记在那里,但在 ssr 上却没有:/
I tried it with async and await, but I still get the error "Uncaught (in promise) TypeError: seoTitle is undefined".我使用 async 和 await 进行了尝试,但我仍然收到错误“未捕获(承诺中)TypeError:seoTitle 未定义”。
(I tried it with a useless await this.getArticle const, in hope that the whole thing waits, this stuff is there, but no) (我用无用的await this.getArticle const试了一下,希望整个事情都等着,这个东西在那里,但没有)
Here my code:这是我的代码:
async head() {
const article = await this.getArticle
const seoTitle = await this.getArticle.seoTitle,
title = await this.getArticle.title,
seoDescription = await this.getArticle.description
return {
title: `"${
seoTitle.length ? seoTitle : title
}"`,
meta: [
{
hid: "description",
name: "description",
content: `${
seoDescription.length
? seoDescription.slice(0, 50)
: seoDescription.slice(0, 50)
}`,
},
],
};
},
To my knowledge, you cannot use async
on head
because it is usually using some static values.据我所知,您不能直接使用
async
,因为它通常使用一些head
值。
And looking at this github answer , it looks like you can use asyncData
to have access to the values you want to input in head
.并查看此github 答案,看起来您可以使用
asyncData
访问要在head
中输入的值。
head() {
return { title: this.info.title }
},
async asyncData ({ params }) {
return axios.get(`/post/${params.id}/info`)
.then((res) => {
return {
info: res.data.info
}
}).catch((err) => {
console.log(err)
})
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.