vue-router和react-router-dom路由传参对照

React
 
1.动态路由传参
<Route exact path="/detail/:id" component={Detail}/>
接收
componentDidMount() {
    console.log( this.props.match.params);
}
2.函数传参(push)加密的地址栏不可见
<button onClick={() => this.props.history.push({ pathname: '/detail', state: { id: 3 } })}>通过函数跳转</button>
接收
componentDidMount() {
    //console.log(this.props.match.params);
    console.log( this.props.history.location.state);
}
不加密(query传参)
this.props.router.push({ path : '/sort' ,query : { name: ' sunny'} })
接收
this.props.location.query.name
 
 
以上方法都可以在 <Link  to={{path=:'/sort',query:{name:'xx'}}}>    传递
 
 
Vue
 
1.动态路由
先在路由那配置
  {
     path: '/describe/:id',
     name: 'Describe',
     component: Describe
   }

 

 
然后在组件中通过
      
 this.$router.push({
     path: `/describe/${id}`,
 })

 

或者 <router-link  link="/describe/2" />
 
 
接收
 
this.$route.params.id
2.通过 name和params配合
先在路由配置 
 
{
     path: '/describe',
     name: 'Describe',
     component: Describe
   } 

  

然后在组件中通过
     
this.$router.push({
          name: 'Describe',
          params: {
            id: id
          }
        })

  

接收
this.$route.params.id
3.通过path和query配合使用
先在路由配置   
{
     path: '/describe',
     name: 'Describe',
     component: Describe
   }

  

 
然后在组件中通过
    
 this.$router.push({
          path: '/describe',
          query: {
            id: id
          }
        })

  

接收
this.$route.query.id

转载于:https://www.cnblogs.com/zhangjixiang123/p/10128074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值