Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React路由器多个参数不起作用_Reactjs_Parameters_Hyperlink_Path_Router - Fatal编程技术网

Reactjs React路由器多个参数不起作用

Reactjs React路由器多个参数不起作用,reactjs,parameters,hyperlink,path,router,Reactjs,Parameters,Hyperlink,Path,Router,我想从path=/users/:login/:repo查看login的repo详细信息页面,但它不起作用并查看UserProfile组件。 点击这里: <Link to={this.props.location.pathname+'/'+item.name}> <div> <h4>{item.name}</h4> <p>{item.description} </p> </div&g

我想从path=/users/:login/:repo查看login的repo详细信息页面,但它不起作用并查看UserProfile组件。 点击这里:

 <Link to={this.props.location.pathname+'/'+item.name}>
   <div>
      <h4>{item.name}</h4>
      <p>{item.description} </p>
   </div>
 </Link>


<Switch>
    <Route exact path="/" component={Contributors}/>
    <Route  path="/users/:login" component={UserProfile}/>
    <Route  path="/users/:login/:repo" component={RepoPage}/>
</Switch>
任何由/users/:login/:repo匹配的url也将由/users/:login匹配,除非您使用确切的限定符

在Switch的上下文中,这意味着每次都会呈现UserProfile,因为它位于第一位

解决办法是改变:

<Route  path="/users/:login" component={UserProfile}/>


您需要颠倒路由的顺序,因为交换机与第一条路由匹配

<Switch>
    <Route exact path="/" component={Contributors}/>
    <Route  path="/users/:login/:repo" component={RepoPage}/>
    <Route  path="/users/:login" component={UserProfile}/>
</Switch>

使用React路由器,作为完全匹配路径前缀的路径也会匹配,因此/users/:login/:repo也会匹配/users/:login,并且由于您使用的是交换机,所以会呈现RepoPage,而不会检查在此之后定义的其他路由

您重定向到的呈现UserProfile的url是什么?
<Switch>
    <Route exact path="/" component={Contributors}/>
    <Route  path="/users/:login/:repo" component={RepoPage}/>
    <Route  path="/users/:login" component={UserProfile}/>
</Switch>