【发布时间】:2017-10-03 08:18:54
【问题描述】:
有几条带参数的路线(例如./user/:user, car/:car/:year)
如果可以使用 react-router (v3),我会尽量避免手动解析 location.pathname。
如何找到与当前 url 位置匹配的路由。 需要类似的东西:
if (router.match_to_route('/user/:user')) {
... do something
}
...
https://github.com/ReactTraining/react-router/blob/v3/modules/matchRoutes.js 中的 matchRoutes 方法可能是我需要的方法。 谢谢。
更新:
可以通过
import { match } from 'react-router';
const routes = router.routes;
match({ routes, location }, (error, redirect, renderProps) => {
const listOfMatchingRoutes = renderProps.routes;
if (listOfMatchingRoutes.some(route => route.path === '/user/:user')) {
...
}
}
https://github.com/ReactTraining/react-router/issues/3312#issuecomment-299450079
【问题讨论】:
标签: javascript reactjs react-router