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>
相关推荐
神秘的猪头3 分钟前
Ajax 数据请求:从零开始掌握异步通信
前端·javascript
黛色正浓11 分钟前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
稀饭5217 分钟前
用changeset来管理你的npm包版本
前端·npm
TeamDev18 分钟前
基于 Angular UI 的 C# 桌面应用
前端·后端·angular.js
Komorebi゛36 分钟前
【CSS】斜角流光样式
前端·css
Irene199143 分钟前
CSS 废弃属性分类总结
前端·css
青莲8431 小时前
Android 事件分发机制 - 事件流向详解
android·前端·面试
musashi1 小时前
用 Electron 写了一个 macOS 版本的 wallpaper(附源码、下载地址)
前端·vue.js·electron
满天星辰1 小时前
Typescript之类型总结大全
前端·typescript
JFChen1 小时前
Web 仔用 Node 像 Java 一样写后端服务
前端