1.配置中加id
//容器 class Wrapper extends React.Component { constructor(props){ super (props) } render() { return ( <div> <Link to='/a/123'>组件A</Link> <br/> <Link to='/b'>组件B</Link> {this.props.children} </div> ) } }
2.获取参数使用:
this.props.match.params.id
组件a接收参数的代码:
class A extends React.Component { constructor (props){ super (props) } render(){ return ( <div> component A 参数是:{this.props.match.params.id} </div> ) } }
3.页面显示
4.完整代码
import React from 'react'; import ReactDOM from 'react-dom'; import {BrowserRouter as Router,Route,Link} from 'react-router-dom'; class A extends React.Component { constructor (props){ super (props) } render(){ return ( <div> component A 参数是:{this.props.match.params.id} </div> ) } } class B extends React.Component { constructor (props){ super (props) } render(){ return <div>component B</div> } } //容器 class Wrapper extends React.Component { constructor(props){ super (props) } render() { return ( <div> <Link to='/a/123'>组件A</Link> <br/> <Link to='/b'>组件B</Link> {this.props.children} </div> ) } } ReactDOM.render( <Router> <Wrapper> <Route path='/a/:id' component={A}/> <Route path='/b' component={B}/> </Wrapper> </Router>, document.getElementById('app') )