react-router-dom

    xiaoxiao2023-10-16  154

    传参方式

    1.通配符传参

    优点:页面刷新,参数不丢失 缺点:只能传递字符串

    <Route path='/path/:name' component={Path}/> <Link to="/path/通过通配符传参">通配符</Link> this.props.match.params.name // 获取参数
    2.query传参

    优点:可以传递对象 缺点:页面刷新,参数丢失

    <Route path='/query' component={Query}/> var query = { pathname: '/query', query: '我是通过query传值 ' } <Link to={query}>query</Link> this.props.location.query // 获取参数
    3.state传参

    优点:可以传递对象 缺点:页面刷新,参数丢失

    <Link to={state}>state</Link> var state = { pathname: '/state', state: '我是通过state传值' } <Route path='/state' component={State}/> this.props.location.state // 获取参数
    最新回复(0)