Vue Router路由的params传值与query传值

Vue Router传值的方法

vue路由传值共有两种方式:1.query 2.params

query
  • 使用query传参,路由配置时path不用带参数

    //路由配置
    {
        name: 'detail',
        path: '/detail',
        component: Detail   
    }
    
    // 使用:
    // 使用name引入路由(三种方式效果相同)
    1. <router-link :to="{name: 'detail',query:{ id:123 }}">点击</router-link>
    2. this.$router.push({name: 'detail',query:{ id:123 }})
    3. this.$router.push('/detail?id=${this.$route.query.id}')
    
    // 使用path引入路由(两种方式效果相同)
    1. <router-link :to="{path: 'detail',query:{ id:123 }}">点击</router-link>
    2. this.$router.push({path: 'detail',query:{ id:123 }})
    
    // 页面显示的结果:http://localhost:8080/#/detail?id=123
    
  • 获取参数值的方法this.$route.query.id

params
  • 路由配置时,path需要带参数

    // 路由配置
    {
        name: 'detail',
        path: '/detail/:id',// 这里需要带参数
        component: Detail
    }
    
    // 使用:
    // 只能使用name引入路由(三种方式效果相同)
    1. <router-link :to="{name: 'detail', params:{ id:123 }}">点击</router-link>
    2. this.$router.push({name: 'detail', params:{ id:123 }})
    3. this.$router.push('/detail/${this.$route.params.id}')
    
    // 页面显示结果:http://localhost:8080/#/detail/123
    
  • 获取参数值的方法this.$route.params.id

两者的区别

  • 路由配置不同
    • query传值: 使用query传参,路由配置时path不用带参数 path: '/detail'
    • param传值: 路由配置时,path需要带参数 path: '/detail/:id'
  • 使用时条件限制不同
    • query传值: query可以和namepath一起使用
    • param传值: params不能与path一起使用
  • 页面显示结果不同
    • query传值: 页面显示的结果:http://localhost:8080/#/detail?id=123
    • param传值: 页面显示结果:http://localhost:8080/#/detail/123
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值