javascript - 如何使用反应路由器 dom v5.0.1 获取参数
问题描述
如何在获取参数之前使用 react router dom v5.0.1 获取参数,但现在我无法获取它们,这是什么问题?
<BrowserRouter>
<Route path='/' exact component={Login} />
<Route path='/register' component={Register} />
<Route path='/home' component={Home} />
<Route path='/all/merchant' component={AllMerchant} />
<Route path='/detail/merchant/:id_merchant' exact component={DetailMerchant} />
<Route path='/detail/merchant/promo/:id_merchant' component={MerchantByPromo} />
<Route path='/promo/voucher/by/merchant/' component={MerchantByPromoDetail} />
<Route path='/all/category' component={Category} />
<Route path='/category/id/:category_id' component={CategoryById} render = {props => <CategoryById {...props} key={this.props.match.params} /> }/>
<Route path='/promo/grid/view' component={PromoGridView} />
{/* params not found */}
<Route path='/promo/grid/view/by/category/:category_id' component={AllPromoGridView} />
<Route path='/detail/promo/:voucher_id/category_id/:category_id' component={PromoGridViewDetail} />
{/* params not found */}
<Route path='/account' component={Account} />
</BrowserRouter>
解决方案
根据 react-router doc,您应该可以Component
通过 props访问它match
:
例子:
// the route
<Route path="/:id" component={Child} />
/*
...
*/
//the component
function Child({ match }) {
return (
<div>
<h3>ID: {match.params.id}</h3>
</div>
);
}
推荐阅读
- python - 从 Azure SSIS 运行 python 脚本
- git - 如何在 Visual Studio 2019 中删除本地 git 存储库?
- geocoding - 这里 api 批量地理编码仍然被接受
- sql - 如何在 SQL Server 中根据身份创建要插入的值?
- java - Spring Boot 中的 PersistenceContext 生命周期
- oracle - Oracle 12c Sysdate 与文本框日期时间的比较
- java - 是否可以使用 MS Java EWS 搜索服务器上的所有邮箱?
- node.js - MVC rest api中的格式化逻辑放在哪里
- regex - How to match regular expression but return the remaining part of the input in Rust Nom?
- css - 如何选择 web 组件的 shadow-dom ::part() 的 :first-child