vue路由传参

本文详细介绍了在Vue中通过query和params两种方式实现组件间参数传递。首先展示了如何在组件1中通过点击事件触发路由跳转,并附带参数;接着说明了在路由配置中如何使用props接收query参数;最后,对比了params传参的方式,包括点击事件触发、路径传参、以及在目标组件中直接访问params参数。
摘要由CSDN通过智能技术生成

vue路由传参

1:query方法:
组件1给组件2传参
1)点击按钮给其他组件传参

 <div class="bigbox">
      <div
        v-for="item in shoppinglist"
        :key="item.id"
        class="box"
        @click="tiaozhuan(item)"
      >
        <!-- <img class="img" :src="item.img" alt="" /> -->
        <h2>编号:{{item.id}}</h2>
        <h2>商品名称:{{ item.name }}</h2>
        <h3>商品价格:{{ item.price }}</h3>
        <p><span>商品描述</span>{{item.goodsDesc}}</p>
      </div>
    </div>

2)在跳转路由时,query携带参数传递

  tiaozhuan(item) {
      this.$router.push({
        path: "/goods/goodlisttotwo",
        query: {
          id: item.id,
          name: item.name,
          ctime:item.ctime,
          price:item.price,
          sellCount:item.sellCount,
          goodsDesc:item.goodsDesc,
          category:item.category,
          rating:item.rating
        },
      });
    },

3)在组件2的router中的index.js中的路由配置使用props接收参数
并将接收到的参数使用别名来接收

{
           path:'/goods/goodlisttotwo',
           component:Goodlisttotwo,
           props($route){
               return {
                 id:$route.query.id,
                 name:$route.query.name,
                 price:$route.query.price,
                 ctime:$route.query.ctime,
                 sellCount:$route.query.sellCount,
                 goodsDesc:$route.query.goodsDesc,
                 category:$route.query.category,
                 rating:$route.query.rating,
               }
           }
         }

4)在组件2中使用接收到的参数
使用时{{$route.query.name}}

<template>
  <el-card class="box-card">
    <div class="box">
      <div class="one">
       <p>商品名称:{{ $route.query.name}}
      </div>
       </div>
       </el-card>
</template>

使用props来接收router文件夹中index.js配置路由的数据别名

methods:{
   props:['id','name','ctime','price','sellCount','goodsDesc','category','rating']
}

2:params方法传参
1)点击按钮给其他组件传参:

      <div class="pa" @click="dianji(params1)">点击params传参</div>

2)跳转路由时传参:

export default {
  data() {
    return {
      params1:'我是params1方法传递的参数'
    }
  },
  methods:{
   dianji(id){
      this.$router.push({
        path:`/ceshi/${id}`
      })
    }
  }

3)在router中index.js中配置路由:

 {
        path:'/ceshi/:id',
        component:Ceshi
      }

4)直接在路由中使用:

<h1>这是params传值的方法{{this.$route.params.id}}</h1>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值