[英]Nuxt: Dynamic head / meta title is undefined on ssr
我有一個 nuxt 項目,其中元標題和描述來自(來自 nuxt/content)。 數據的異步獲取在索引中進行,並通過 getter 在子組件中接收。
在生成時,元標記在那里,但在 ssr 上卻沒有:/
我使用 async 和 await 進行了嘗試,但我仍然收到錯誤“未捕獲(承諾中)TypeError:seoTitle 未定義”。
(我用無用的await this.getArticle const試了一下,希望整個事情都等着,這個東西在那里,但沒有)
這是我的代碼:
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)
}`,
},
],
};
},
據我所知,您不能直接使用async
,因為它通常使用一些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.