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

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!!!

另外,二者还有点区别:
    1. 接收参数
// query通过this.$route.query接收参数
created () {
    const id = this.$route.query.id;
}
 
// params通过this.$route.params来接收参数
created () {
    const id = this.$route.params.id;
}
    1. 切换路由
// 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. routerthis.route有何区别?

在控制台打印两者可以很明显的看出两者的一些区别:

clipboard.png

  • 1. r o u t e r 为 V u e R o u t e r 实 例 , 想 要 导 航 到 不 同 U R L , 则 使 用 router为VueRouter实例,想要导航到不同URL,则使用 routerVueRouterURL使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);
                  })
                })
              }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DuebassLei

请我吃颗棒棒糖吧~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值