简体   繁体   English

反应路由器相同的路由不同的参数

[英]React router same route different parameters

i have two routes for different pages like:我有两条不同页面的路线,例如:

<Switch>
  <Route exact path="/:category/:subCategory?/:page?" component={Category} /> 
  <Route exact path="/:category/:subCategory?/:product/:page?" component={Product} />
</Switch>

this link: app.com/headphones/airpod3 takes me to Category view because route does not understand that this is the name of a product or category.此链接: app.com/headphones/airpod3将我带到类别视图,因为路由不理解这是产品或类别的名称。

my problem is that some categories may have subcategories but some do not.我的问题是某些类别可能有子类别,但有些没有。 And i have pagination the optional:page parameter我有分页可选:页面参数

i need route my app like:我需要像这样路由我的应用程序:
app.com/usb-cable/light-cable/1 or app.com/headphones/apple/airpod3 to take me to Product page app.com/usb-cable/light-cable/1app.com/headphones/apple/airpod3带我到产品页面
app.com/headphones or app.com/headphones/apple to Category view app.com/headphonesapp.com/headphones/apple到类别视图

how can i route my app like this?我怎样才能像这样路由我的应用程序?

You need to restrict your routes especially page params to accept number like below /:page(\d+)?您需要限制您的路线,尤其是页面参数以接受如下数字 /:page(\d+)? this will only allow numbers thus when you hit app.com/headphones/airpod3 it will go to product page not category这将只允许数字,因此当您点击 app.com/headphones/airpod3 时,它将 go 到产品页面而不是类别

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

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