Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gatsby 盖茨比:生成嵌套的动态路由_Gatsby - Fatal编程技术网

Gatsby 盖茨比:生成嵌套的动态路由

Gatsby 盖茨比:生成嵌套的动态路由,gatsby,Gatsby,我正在尝试为以下URL生成一些仅限客户端的路由: /products/anythis/this page 盖茨比生成的URL类似于/products/anything/ 我试过: exports.onCreatePage=async({page,actions})=>{ const{createPage}=actions if(page.path.match(/^\/product/){ page.matchPath='/product/*' createPage(第页) } } 没有用 还有

我正在尝试为以下URL生成一些仅限客户端的路由:

/products/anythis/this page

盖茨比生成的URL类似于
/products/anything/

我试过:

exports.onCreatePage=async({page,actions})=>{
const{createPage}=actions
if(page.path.match(/^\/product/){
page.matchPath='/product/*'
createPage(第页)
}
}
没有用

还有一个更复杂的正则表达式

if(page.path.match(/^product\/.\/.*/)){
page.matchPath=/^product\/.*/*/
createPage(第页)
}

在onCreatePage函数中,您可以动态创建页面的url:

// The all allWcProduct is a custom node I've added, but could be replaced with 
another node you have on your site instead.
const path = require("path")
const pageQuery = `
    {
      allWcProduct{
        edges{
          node{
            slug
            name
          }
        }
      }
    }
    `

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions

  return new Promise((resolve, reject) => {
    graphql(pageQuery).then(results => {

      if (results.errors) {
        console.log(results.errors)
        reject(results.errors)
      }
      results.data.allWcProduct.edges.forEach(({ node }) => {
        createPage({
          path: `/products/${node.slug}`,
          component: path.resolve(`./src/components/products/productDetailPage.tsx`),
          context: {
            slug: node.slug // send this to the component itself as a prop
          }
        })
      })
    })
    resolve()
  })

}

如上所述,您还可以使用该插件进行更基本的使用。

在onCreatePage函数中,您可以动态创建页面的url:

// The all allWcProduct is a custom node I've added, but could be replaced with 
another node you have on your site instead.
const path = require("path")
const pageQuery = `
    {
      allWcProduct{
        edges{
          node{
            slug
            name
          }
        }
      }
    }
    `

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions

  return new Promise((resolve, reject) => {
    graphql(pageQuery).then(results => {

      if (results.errors) {
        console.log(results.errors)
        reject(results.errors)
      }
      results.data.allWcProduct.edges.forEach(({ node }) => {
        createPage({
          path: `/products/${node.slug}`,
          component: path.resolve(`./src/components/products/productDetailPage.tsx`),
          context: {
            slug: node.slug // send this to the component itself as a prop
          }
        })
      })
    })
    resolve()
  })

}

如上所述,您还可以使用插件进行更基本的使用。

我找到了一个解决方案,在动态slug之后创建嵌套路由。在gatsby-node.js文件中:

  exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions

  const { data } = await graphql(`
    query {
      //your query here
    }
  `)

  data.allProducts.edges.forEach(edge => {
    const slug = edge.node.productId
     
    //create your dynamic page

    createPage({
      path: `/products/${slug}/`,
      component: require.resolve(
        "./src/components/products/templates/product-template/ProductTemplate.tsx"
      ),
      context: { productData: edge.node },
    })

    //create nested route in it

    createPage({
      path: `/products/${slug}/something/`,
      component: require.resolve(
        "./src/components/trainings/templates/something/something.tsx"
      ),
      context: { productData: edge.node },
    })
  })
}

我找到了一个解决方案,在动态slug之后创建嵌套路由。在gatsby-node.js文件中:

  exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions

  const { data } = await graphql(`
    query {
      //your query here
    }
  `)

  data.allProducts.edges.forEach(edge => {
    const slug = edge.node.productId
     
    //create your dynamic page

    createPage({
      path: `/products/${slug}/`,
      component: require.resolve(
        "./src/components/products/templates/product-template/ProductTemplate.tsx"
      ),
      context: { productData: edge.node },
    })

    //create nested route in it

    createPage({
      path: `/products/${slug}/something/`,
      component: require.resolve(
        "./src/components/trainings/templates/something/something.tsx"
      ),
      context: { productData: edge.node },
    })
  })
}

您好,我正在尝试创建仅用于客户端的动态路由。不是静态呈现的页面。我明白了,那么你需要像Next.js这样的东西,盖茨比本质上是一个静态站点生成器。上面的代码仅根据您通过GraphQL提供的数据生成动态静态页面。@SpencerBigum:这是一个问题。@magicspon是的,我想做类似的事情,但没有找到任何解决方法。如果你能做到这一点,请让我知道你好,我正在尝试创建动态客户端只路由。不是静态呈现的页面。我明白了,那么你需要像Next.js这样的东西,盖茨比本质上是一个静态站点生成器。上面的代码仅根据您通过GraphQL提供的数据生成动态静态页面。@SpencerBigum:这是一个问题。@magicspon是的,我想做类似的事情,但没有找到任何解决方法。如果你能做到,请告诉我你能找到解决办法吗?恐怕不行…你能找到解决办法吗?恐怕不行。。。