如何在反应中通过链路路由器获取程序值

How to get the prams value over the link router in react

本文关键字:路由器 链路 获取 程序      更新时间:2023-09-26

我在主组件中有这个链接如何获得播放器组件中的值:

 <Link to="/player" params={{ testvalue: "hello" }} 
 className="btn">watch</Link>
class Player extends React.Component{
    render(){
        alert(this.params.testvalue);
        return(
            <div></div>
              );
    }
}

是一个路由文件

    <Route path="/" component={App}>
      <IndexRoute component={main}/>
      <Route path="/player" component={Player}/>
    </Route>

使用react router传递一个location对象作为props,应该能够使用

访问查询。

this.props.params.testvalue

在你的播放器组件

您的代码的问题是,您正在传递一个参数

 <Link to="/player" params={{ testvalue: "hello" }} 
 className="btn">watch</Link>

但是你没有在路由中收到它。更改你的路由,如

<Route path="/" component={App}>
  <IndexRoute component={main}/>
  <Route path="/player/:value" component={Player}/>
</Route>

然后使用

this.props.params.testvalue