reactjs - 有什么方法可以使用模板组件/页面使用 Gatsby 创建动态 slugs?

标签 reactjs gatsby slug strapi

我对 Gatsby 很陌生,我想知道是否可以创建自定义路由(slugs)作为模板。用例是:

  • 访问 /articles/name-of-the-article路线
  • 服务组件Article.js填充了使用 name-of-the-article 从 API(即 Strapi headless CMS)检索到的信息蛞蝓。
  • 最佳答案

    经过一些调查,我发现使用 gatsby-plugin-create-client-paths 有更简单的方法来做到这一点。 .
    您需要做的就是使用 yarn 安装它或 npm然后在 gatsby-config.js你将添加这些:

    {
          resolve: `gatsby-plugin-create-client-paths`,
          options: { prefixes: [`/articles/*`] },
    },


    这意味着每个请求都像这样:/articles/the-slug将请求 articles.js页面和使用 Link由 Gatsby 提供,你可以通过 state 传递 props像这样的 Prop :
    <Link to="/articles/the-slug" state={{ slug: "the-slug", ...etc }}>Anchor Text</Link>
    这样,src/pages/articles.js成为前缀为 /articles 的 slug 的模板页面.

    关于reactjs - 有什么方法可以使用模板组件/页面使用 Gatsby 创建动态 slugs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59268072/

    相关文章:

    javascript - Reactjs Link 不适用于 IE11 但适用于 Edge 和其他浏览器

    javascript - 如何让 Material-UI 的 <TextField/> 像 ReactJS 中的 &lt;input/> 一样使用 ref ='' 正确返回?

    symfony - 手动生成学说 slug

    python - 最佳实践 : prepopulate SlugField in Django (without Admin module)

    reactjs - Gatsby 和 graphql : filtering for a specific single image

    node.js - Heroku Slug 大小太大 - nodejs

    javascript - 类型错误 : Cannot read property 'value' of null in React Form

    javascript - Electron 加载 index.html 但不加载其余的包文件

    GatsbyJS 中的 GraphQL 过滤器

    javascript - Gatsby 类型错误 : Cannot read property 'allWordpressAcfOptions' of undefined