首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Gatsby配置中,如何将元数据对象隔离到它自己的文件中,但仍然能够在GraphQL中使用它?

在Gatsby配置中,如何将元数据对象隔离到它自己的文件中,但仍然能够在GraphQL中使用它?
EN

Stack Overflow用户
提问于 2021-01-28 18:50:54
回答 1查看 859关注 0票数 1

学习盖茨比,我参考了文档,了解了siteMetadata对象。我不喜欢乱堆一个文件,我想看看是否可以将元数据隔离到单独的文件中,并将其放入其中,但是我遇到了一个GraphQL错误。

结构

在根目录中,我创建了一个目录:

代码语言:javascript
复制
/config

menuLinks.js:

代码语言:javascript
复制
module.exports = [
  {
    name: `Home`,
    link: `/`,
    img: 'a.png',
  },
  {
    name: `Articles`,
    link: `/articles`,
    img: 'b.png',
  },
  {
    name: `About`,
    link: `/about`,
    img: 'c.png',
  },
  {
    name: `Events`,
    link: `/events`,
    img: 'e.png',
  },
]

siteMetadata.js:

代码语言:javascript
复制
const menuLinks = require('./menuLinks')

module.exports = [
  {
    title: `Project`,
    titleTemplate: `%s · a starting point`,
    author: {
      name: `foo bar`,
      summary: `Enter the foo`,
    },
    description: `Just fooling around`,
    url: `https://something.io`,
    logo: `/logo.png`,
    twitter: `foobar`,
    menuLinks,
  },
]

将其引入gatsby-config.js中。

代码语言:javascript
复制
const siteMetadata = require('./config/siteMetadata')

module.exports = {
  siteMetadata,
  plugins: [
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-postcss`,
    `gatsby-plugin-css-customs`,
    `gatsby-plugin-styled-components`,
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/content/images/`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `events`,
        path: `${__dirname}/content/events/`,
      },
    },
    {
      resolve: `gatsby-transformer-yaml`,
      options: {
        typeName: `Event`, // a fixed string
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `articles`,
        path: `${__dirname}/content/articles/`,
      },
    },
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {
              // It's important to specify the maxWidth (in pixels) of
              // the content container as this plugin uses this as the
              // base for generating different widths of each image.
              maxWidth: 1080,
              quality: 100,
            },
          },
        ],
      },
    },
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `Project`,
        short_name: `Project`,
        start_url: `/`,
        background_color: `#ffffff`,
        theme_color: `#ffffff`,
        // Enables "Add to Homescreen" prompt and disables browser UI (including back button)
        // see https://developers.google.com/web/fundamentals/web-app-manifest/#display
        display: `standalone`,
        icon: `static/icon.png`, // This path is relative to the root of the site.
      },
    },
    `gatsby-plugin-offline`,
  ],
}

错误

代码语言:javascript
复制
 28:11  error  Cannot query field "menuLinks" on type "SiteSiteMetadata"  graphql/template-strings

代码语言:javascript
复制
76:9  error  Cannot query field "titleTemplate" on type "SiteSiteMetadata"  graphql/template-strings

研究

问题

我已经用.cache清理了npm run clean和公共目录。在gatsby-config.js中,我如何将元数据对象隔离到它自己的文件中,并能够在GraphQL中引用它呢?

编辑

回答建议实现扩展操作符之后:

错误

src/components/seo.js 76:9错误不能查询"titleTemplate“类型的"SiteSiteMetadata”graphql/template-字符串

代码

./config/siteMetadata.js:

代码语言:javascript
复制
const links = require('./menuLinks')

module.exports = [
  {
    title: `Project`,
    titleTemplate: `%s · a starting point`,
    author: {
      name: `foo bar`,
      summary: `Enter the foo`,
    },
    description: `Just fooling around`,
    url: `https://something.io`,
    logo: `/logo.png`,
    twitter: `foobar`,
    menuLinks: {
      ...links,
    },
  },
]

./config/menuLinks.js:

代码语言:javascript
复制
module.exports = [
  {
    name: `Home`,
    link: `/`,
    img: 'a.png',
  },
  {
    name: `Articles`,
    link: `/articles`,
    img: 'b.png',
  },
  {
    name: `About`,
    link: `/about`,
    img: 'c.png',
  },
  {
    name: `Events`,
    link: `/events`,
    img: 'e.png',
  },
]

gatsby-config.js

代码语言:javascript
复制
const metadata = require('./config/siteMetadata')

module.exports = {
  siteMetadata: {
    ...metadata,
  },
  plugins: [`gatsby-plugin-react-helmet`],
}

graphQL来自seo.js:

代码语言:javascript
复制
const query = graphql`
  query SEO {
    site {
      siteMetadata {
        defaultTitle: title
        titleTemplate
        defaultDescription: description
        siteUrl: url
        defaultImage: logo
        twitterUsername: twitter
      }
    }
  }
`
EN

回答 1

Stack Overflow用户

发布于 2021-01-29 14:03:45

我认为您有更好的方法来使用菜单导航,而不是将它们放在siteMetadata对象中,比如使用useStaticQuery钩子来使导航可重用。但是,如果要合并两个对象,可以很容易地执行以下操作:

代码语言:javascript
复制
// gatsby-config.js
const siteMetadata = require('./config/siteMetadata')
const menuLinks = require('./menuLinks')


module.exports = {
  mergedObject,
  plugins: [
    ...
  ],
}

或直接:

代码语言:javascript
复制
// gatsby-config.js
const siteMetadata = require('./config/siteMetadata')
const menuLinks = require('./menuLinks')

const mergedObject={...siteMetadata, ...menuLinks}

module.exports = {
  siteMetadata{
   ...siteMetadata,
   ...menuLinks
  },
  plugins: [
    ...
  ],
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65943448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档