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

前提:有完整的路由规则

1.源页面

html 复制代码
<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.目标页面

html 复制代码
<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 小时前
web第三次作业
前端·javascript·css
小王不会写code3 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luoluoal4 小时前
基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
vue.js·spring boot·宠物
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome