加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-router 学习笔记之Link带参数传值

发布时间:2020-12-15 07:13:09 所属栏目:百科 来源:网络整理
导读:1、 通过to='xx/xx'直接条状Link to={`/my/${myId}/info}点击/Link 2、 to=对象,带参数跳转(pathname,query,hash,state(额外数据)),注意:这些参数都被存放到this.props.location中 liLink to={{pathname:"/select",hash:'#ahash',query:{foo: 'bar',boo

1、通过to='xx/xx'直接条状<Link to={`/my/${myId}/info}>点击</Link>

2、to=对象,带参数跳转(pathname,query,hash,state(额外数据)),注意:这些参数都被存放到this.props.location中

<li><Link to={{pathname:"/select",hash:'#ahash',query:{foo: 'bar',boo:'boz'},state:{data:'miao'} }} activeClassName="GlobalNav-active">精选</Link></li>

3、to=函数,注册到路由跳转事件中,每一次路由变化,都会执行该函数,并经最新的location作为参数,

<Link to={location => ({ ...location,query: { name: 'ryan' } })}>
Hello
</Link>

4、不使用Link,在函数内直接操作router

旧版本:由于router只用的context传递路由信息,因此每一个组件都可以轻易的通过this.context.router获取路由

      新版本:router被放置在this.props中,通过this.props.router可以获取路由

      注意:push与replace的区别,一个是添加,一个是替换,历史记录中被替换的已经不存在了,所以浏览器回退不到替换前的页面。


changeRouter = () => { console.log(this.props) // this.props.router.push('/follow'); // this.props.router.push({ // pathname:'/follow',// state:{name:'xxx'},// query: {foo: 'bar'} // }) // this.props.router.replace('/follow'); this.props.router.replace({ pathname: '/follow',query: {foo:'bar'} }) } 复制代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读