繁体   English   中英

如何在nextjs的getInitialProps stateles组件中访问路由器参数

[英]how to access router params in getInitialProps stateles component in nextjs

我正在使用 nextjs 并且我在使用 getInitialProps 时遇到问题。

我想使用我在 getInitialProps 的 Post Component 中创建的变量。 你可以看到我的男女同校:

const Post = props => {
  const router = useRouter()
   let id = router.query.id  
   let urlTitle = router.query.title

  return (
    <Layout>
       //somthing
    </Layout>
  )
}


Post.getInitialProps = async () => {

// i want to use {id} and {urlTitle} here

}

我该怎么做 ?!!!

您应该能够从 getInitialProps 访问 {query}

const Post = ({id, urlTitle}) => {

  return (
    <Layout>
       //somthing
    </Layout>
  )
}


Post.getInitialProps = async ({ query }) => {

  const { id, title } = query

  return {
    id,
    urlTirle: title
  }

}

暂无
暂无

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

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