【发布时间】:2025-12-07 03:40:01
【问题描述】:
如果我有这样的事情:
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
我的路线配置如下:
export default [{
path: '/',
exact: true,
main: Home
},
{
path: '/:someId',
exact: true,
main: Profile
},
{
path: '*',
main: NotFound
}];
app 只是路由和其他组件的包装器,例如:
class App extends Component {
render() {
return (
<div>
<Header />
<Switch>
{routes.map((route, i) => <Route exact key={i} component={route.main} path={route.path}/>)}
</Switch>
<AnotherComponent {...this.props} />
</div>
);
}
}
AnotherComponent 有没有办法使用 match.params 或公开这些?我已经尝试用 withRouter 包装组件,并将其添加为没有路径匹配的路由,例如:
<Route component={AnotherComponent} />
当路由为 /:someId 时,它会同时渲染 Profile 和 AnotherComponent,但 AnotherComponent 的 match.params 为空:/。
这可能吗? 谢谢!
【问题讨论】:
标签: javascript reactjs react-router-redux react-router-v4 react-router-dom