Vue3点击按钮实现跳转页面并携带参数

前提:有完整的路由规则

1.源页面

<template>
  <div>
    <h1>源页面</h1>
    <!--通过js代码跳转-->
 <template #default="scope">
    <button @click="toTargetView(scope.row)">点击跳转携带参数</button>
        </template>

  </div>
</template>

<script setup>
//引入路由组件
import router from '@/router/index.js'
import {ref} from 'vue'

//可以定义参数
const testParam= ref('Test')

const toTargetView = () => {
  router.push({
    path: 'prmbillingcancel',
    query: {
      testParam: testParam.value,
      row: JSON.stringify(row),//这里要JSON序列化一下
    }
  })
}


</script>

<style scoped>

</style>

目标页面这里 path里面写自己的路由编码,传了当前行数据和自定义参数,当前行参数要序列化

2.目标页面

<template>
  <div>
    <h1>目标页面</h1>

    用户名:{{ username }}
   
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {useRoute} from 'vue-router'

const route = useRoute()
//接收自定义参数
const username = ref('')
//接收当前行数据
const item = ref<any>(null);


//使用钩子函数接收来自路由的参数
onMounted(() => {
  username.value = route.query.username
  item.value = JSON.parse(route.query.row as string);//这里反序列化获取参数
  console.log("row",item.value);

})
</script>

<style scoped>

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值