1. 程式人生 > >React-頁面路由參數傳遞的兩種方法

React-頁面路由參數傳遞的兩種方法

route react 16px oca ams list col 不能 strong

list頁->detail頁

方法一:路由參數

路由導航:

用“/”

<Link to={‘/detail/‘+item.get(‘id‘)} key={index}>

路由map:

加"/:id"

<Route exact path="/detail/:id" component={Detail} />

detail頁獲取參數:

準確的獲取到id,不需要做處理

this.props.match.params.id

方法二:查詢參數

路由導航:

用“?”

<Link to={‘/detail?‘+item.get(‘id‘)} key={index}>

路由map:

不加"/:id"

<Route exact path="/detail" component={Detail} />

detail頁獲取參數:

不能準確的獲取到id,需要做處理

this.props.location.search

React-頁面路由參數傳遞的兩種方法