简体   繁体   English

如何在带有冒号的ReactJS中使用动态路由

[英]How to use dynamic routing in ReactJS with a colon

I want to change my routing according to product id, which is dynamic. 我想根据产品ID(动态)更改路由。

I found this solution of variables using a colon. 我使用冒号找到了这种变量的解决方案。

path: "/:product_id" component: ProductDetail

How can I use product_id in my components. 如何在组件中使用product_id

As explained in the docs , you can access data from the params object on the match prop. docs中所述 ,您可以从match道具上的params对象访问数据。

In your case, it would be: match.params.product_id . 在您的情况下,它将是: match.params.product_id For example: 例如:

function ProductDetail({ match }) {
  return (
    <div>
      <h3>PID: {match.params.product_id}</h3>
    </div>
  );
}

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

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