如何在 Next.js 13 中设置动态元关键字和作者

问题描述 投票:0回答:1

我尝试了这个,但它不起作用,在官方文档中他们只给出了动态标题和描述的示例,请任何人帮助我。

我试过这个:

export const metadata = {
  title: {
    default: 'HealthCare Biodiversity',
    template: '%s | HealthCare Biodiversity'
  },
  description: {
    default: 'The Dynamic & Powerful Blog',
    template: '%s | HealthCare Biodiversity'
  },
  referrer: 'origin-when-cross-origin',
  keywords: {
    default: ['Next.js', 'React', 'JavaScript'],
    template: [ '%s']
  },
  authors: {
    default: [{ name: 'Seb' }],
    template: [  { name: '%s' }]
  }
}

此代码位于我的新应用程序路由器的layout.js 中。

我提到了我的尝试和我的期望。请帮助我。

next.js metadata next.js13 app-route
1个回答
0
投票

是的,这是完全可能的。我尝试过将 keywordsauthor 元数据添加到我的 Next.js 13.4 项目应用程序中(使用应用程序路由器)。

metadata
变量对象中,
keywords
属性需要定义为字符串数组,而不是对象(如您发布的示例中所示)。而且,
authors
属性也需要是一个数组,但是是具有某些属性的对象;
name
url
,这两个属性都是可选的。请参阅下面的示例片段:

export const metadata = {
  title: {
    default: 'HealthCare Biodiversity',
    template: '%s | HealthCare Biodiversity'
  },
  description: {
    default: 'The Dynamic & Powerful Blog',
    template: '%s | HealthCare Biodiversity'
  },
  referrer: 'origin-when-cross-origin',
  keywords: ['Next.js', 'React', 'JavaScript', 'HealthCare', 'Biodiversity'],
  authors: [
    { name: 'Seb', url: 'https://github.com/arshcode' }
  ]
}

文档来源:https://nextjs.org/docs/app/api-reference/functions/generate-metadata#basic-fields

© www.soinside.com 2019 - 2024. All rights reserved.