反应路由器传递参数。如何?

时间:2015-08-02 11:03:32

标签: reactjs params react-router

拥有以下React Router

const AppRoutes = (
  <Route path="/" handler={Properties}>
    <DefaultRoute handler={PropertyList} />
    <Route path="property/:propId" handler={PropertyDetail}/>
    <NotFoundRoute handler={NotFound} />
  </Route>);

Router.run(AppRoutes, Router.HashLocation, (Root) => {
  React.render(<Root />, document.getElementById('filter-content'));
});

我尝试在子Component中构建动态链接,这里我有一个测试

<Link to="/property/" params={{ propId: "123"}} ><img src={this.props.data.picture} 
                         data-srcset="http://placehold.it/350x150"  alt="" className="lazyload auto-height" 
                                 data-sizes="auto"/>
                    </Link>

但是点击propId没有通过的链接,我做错了什么?

1 个答案:

答案 0 :(得分:7)

为了使其正常工作,您需要在链接组件的属性中使用路由名称,否则路由器无法知道您的意思和路径定义因此如何处理 propId 参数。

首先,为路线定义名称

<Route name="property" path="property/:propId" handler={PropertyDetail}/>

然后在生成链接时使用该名称:

<Link to="/property/:propId" params={{ propId: "123"}} >
相关问题