如果我有这样的事情:
<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,例如:
<Route component={AnotherComponent} />
当路由为 /:someId 时,它会同时渲染 Profile 和 AnotherComponent,但 AnotherComponent 的 match.params 为空:/。
这可能吗?谢谢!