2016-09-28 36 views
0

我在主要成分此鏈接如何獲得播放器組件值:如何在鏈路的路由器獲得童車值反應

<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> 
+0

爲了幫助你在將來使用這個參數,你可以看看在鉻上的反應devtool和你可以在鏈接中的所有道具只是一個提示 – EQuimper

回答

0

與反應路由器它傳遞一個位置向下對象作爲道具,應該能夠訪問使用查詢

this.props.params.testvalue

在你播放器組件

+0

其顯示一個空值 –

+0

沒有測試值查詢:對象: –

+0

誤讀了原始問題。更新的答案。 'this.props.params'應該顯示'testvalue'在那裏可用,並且可以執行'this.props.params.testvalue' – finalfreq

0

與您的代碼的問題是,你傳遞一個PARAM與

<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