踩坑:为什么 Nuxt.js 应用会出现重复的 Meta 标签?

为了避免子组件中的meta标签不能正确覆盖父组件中相同的标签而产生重复的现象,建议利用 hid 键为meta标签配一个唯一的标识编号。

拿下面的例子来说,对于描述 meta 标签, 我们给 hid 属性设定一个唯一的标识值为:description, 当有组件定义了相同 hid 的 meta 标签时, vue-meta 将知道覆盖父级的配置。

在应用配置文件 nuxt.config.js 中配置默认 meta:

...head: {
    title: 'starter',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { name: 'keywords', content: 'keyword 1, keyword 2'},
      { hid: 'description', name: 'description', content: 'This is the generic description.'}
    ],
  },
...

在页面组件中利用 hid 来覆盖指定的 meta 配置:

export default {
  head () {
    return {
      title: `Page 1 (${this.name}-side)`,
      meta: [
        { hid: 'description', name: 'description', content: 'Page 1 description' }
      ]
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_42224055/article/details/106279405