简体   繁体   English

NuxtJS 页面标题不变

[英]NuxtJS page title not change

I'm working on a nuxtJS project and not much know about nuxt or vue, what I want is change page title, but it show one title for all page, that title belong to one component, I removed that title and now it show nothing.我正在做一个 nuxtJS 项目,对 nuxt 或 vue 了解不多,我想要的是更改页面标题,但它为所有页面显示一个标题,该标题属于一个组件,我删除了该标题,现在它什么也没显示. I put this code in header component我将此代码放在 header 组件中

<script>
    export default {
    name: "header",
    head () {
        return {
            title: this.title
        }
    },
    data () {
        return {
            title: 'Hello World!'
        }
    }
    }
</script>

and in nuxtjs config:在 nuxtjs 配置中:

module.exports = {
  head: {
    title: pkg.name
}
...
}

What I want, show title of each page dynamically.我想要的是动态显示每个页面的标题。

Your nuxt.config.js override title set in your page.您的nuxt.config.js覆盖页面中设置的标题。

You should use titleTemplate in nuxt.config.js :你应该在nuxt.config.js使用nuxt.config.js

head: {
  titleTemplate(titleChunk) {
    return titleChunk ? titleChunk : pkg.name
  }
}

By this way, you can also format title for every page with you site name:通过这种方式,您还可以使用站点名称为每个页面设置标题格式:

head: {
  titleTemplate(titleChunk) {
    return titleChunk ? `{pkg.name} - ${titleChunk}` : pkg.name
  }
}

titleChunk come from head.title of your page like you already do. titleChunk来自您页面的head.title ,就像您已经做的那样。

There are only option to set the title dynamic base on your pages is like override the head function Let me show you one example here只有在您的页面上设置标题动态基础的选项就像覆盖头部功能让我在这里向您展示一个示例

export default {
  ...
  head () {
    return {
      title: 'YOUR PAGE TITLE',
      meta: [
        { hid: 'og-title', property: 'og:title', content: 'YOUR PAGE TITLE },
        ...
      ]
    }
  }
}

To prevent the overwrite parent meta fields, your hid should be unique为了防止覆盖父元字段,您的 hid 应该是唯一的

For more information, please visit the official document of the NuxtJs: https://nuxtjs.org/api/pages-head/更多信息请访问NuxtJs官方文档: https ://nuxtjs.org/api/pages-head/

I am using nuxt version 2.15.7 .我正在使用nuxt version 2.15.7 This syntax is working correctly for me:这种语法对我来说是正确的:

nuxt.config.js file nuxt.config.js 文件

 head: { titleTemplate: '%s', title: 'info-book', // the title you define. you must put your package-name here, if you want that name. }

And then in the component that you want to define title dynamically, you have this code:然后在要动态定义标题的组件中,您有以下代码:

 head() { return { title: "info-book - " + this.titleHead, } }, data () { return { titleHead: "dynamic-title" } },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM