Vue动态路由传参和监听路由

本文详细介绍了Vue中动态路由传参的两种方式:query和params。query方式类似于GET请求,参数在地址栏可见且刷新不丢失;params方式类似于POST请求,参数不显示且刷新后消失。在实际应用中,根据需求选择合适的方式。同时,文章还提到了$router和$route的区别,以及如何监听和响应路由变化,确保组件数据的正确更新。最后,给出了动态路由传参的实战例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vue动态路由传参
query传参
params传参
//定义Detail路由

{
     path: '/detail/:id',
     name: 'Detail'
     component: () => import('@/views/Detail.vue')
}


1.query方式传参和接收参数
传参:

this.$router.push({
        path:'/detail/:id',
        query:{
          id:id
        }
      })


接收参数:

this.$route.query.id
Tips:

传参是this.r o u t e r , 接 收 参 数 是 t h i s . router,接收参数是this.router,接收参数是this.route,这里千万要看清了!!!

2.params方式传参和接收参数
传参:

this.$router.push({
        name:'Detail',
        params:{
          id:id
        }
      })


接收参数:

this.$route.params.id
Tips:

params传参,push里面只能是 name:‘xxxx’,不能是path:’/xxx’,因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!

另外,二者还有点区别:
接收参数
// query通过this.$route.query接收参数
created () {
    const id = this.$route.query.id;
}
 
// params通过this.$route.params来接收参数
created () {
    const id = this.$route.params.id;
}
切换路由
// query通过path切换路由
<router-link :to="{path: 'Detail', query: { id: 1 }}">前往Detail页面</router-link>
// params通过name切换路由
<router-link :to="{name: 'Detail', params: { id: 1 }}">前往Detail页面</router-link>复制代码
简单说query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数,浏览器刷新页面不会消失,而params相当于post请求,参数不会在地址栏中显示,浏览器刷新页面后消失。

3.this.r o u t e r 和 t h i s . router和this.router和this.route有何区别?
在控制台打印两者可以很明显的看出两者的一些区别:

1.r o u t e r 为 V u e R o u t e r 实 例 , 想 要 导 航 到 不 同 U R L , 则 使 用 router为VueRouter实例,想要导航到不同URL,则使用router为VueRouter实例,想要导航到不同URL,则使用router.push方法。
2.$route为当前router跳转对象,可以获取name、path、query、params等。
Vue监听路由
方式一:监听$router
复用组件时,想对路由参数的变化作出响应的话,你可以简单地 watch(监测变化) $route 对象:

  watch: {
    '$route' (to, from) {
      // 对路由变化作出响应...
    }
  }
方式二:唯一值 key 属性
Vue 为你提供了一种方式来声明“这两个元素是完全独立的——不要复用它们”。只需添加一个具有唯一值的 key 属性即可

<router-view :key="key"></router-view>

computed: {
    key() {
        return this.$route.name !== undefined? this.$route.name +new Date(): this.$route         
        +new Date()
    }
 }


使用computed属性和Date()可以保证每一次的key都是不同的,这样就可以如愿刷新数据了。

实践
1. 定义路由

      {
          path: '/hse/problem/prMain/deal/:id',
          component: () => import('@/views/hse/Problem/PrDeal.vue'),
          meta: {
            keepAlive: true
          }
        },

2. 动态路由传参
   

 handleDeal(id){
              this.$router.push(
                {
                  path: `/hse/problem/prMain/deal/${id}`,
                  params: {id: id}
                }
              )
            }


3. 监听路由
       

  watch:{
           //监听路由
           $route(){
             if(this.$route.params!==null){
               this.paramId = this.$route.params.id;
             }
           },
            paramId(newVal,oldVal){
              if(newVal !== undefined && newVal !== null){
                  //初始化数据    
                  this.init();
              }
            }
          }


4. init方法初始化数据

methods:{
   //初始化数据
   init(){
      let vm = this;
      vm.$nextTick(()=>{
         vm.$axios.get(`/hse/sim/prProblem/v1/get/${vm.dataId}`).then(reply=>{
            vm.form = reply.data;
         }).catch(e=>{
            vm.$toast.fail(e);
         })
       })
    }
}

————————————————
版权声明:本文为CSDN博主「DuebassLei」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37903882/article/details/105628626

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值