当前位置:   article > 正文

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

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

前提:有完整的路由规则

1.源页面

  1. <template>
  2. <div>
  3. <h1>源页面</h1>
  4. <!--通过js代码跳转-->
  5. <template #default="scope">
  6. <button @click="toTargetView(scope.row)">点击跳转携带参数</button>
  7. </template>
  8. </div>
  9. </template>
  10. <script setup>
  11. //引入路由组件
  12. import router from '@/router/index.js'
  13. import {ref} from 'vue'
  14. //可以定义参数
  15. const testParam= ref('Test')
  16. const toTargetView = () => {
  17. router.push({
  18. path: 'prmbillingcancel',
  19. query: {
  20. testParam: testParam.value,
  21. row: JSON.stringify(row),//这里要JSON序列化一下
  22. }
  23. })
  24. }
  25. </script>
  26. <style scoped>
  27. </style>

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

2.目标页面

  1. <template>
  2. <div>
  3. <h1>目标页面</h1>
  4. 用户名:{{ username }}
  5. </div>
  6. </template>
  7. <script setup>
  8. import {onMounted, ref} from 'vue'
  9. import {useRoute} from 'vue-router'
  10. const route = useRoute()
  11. //接收自定义参数
  12. const username = ref('')
  13. //接收当前行数据
  14. const item = ref<any>(null);
  15. //使用钩子函数接收来自路由的参数
  16. onMounted(() => {
  17. username.value = route.query.username
  18. item.value = JSON.parse(route.query.row as string);//这里反序列化获取参数
  19. console.log("row",item.value);
  20. })
  21. </script>
  22. <style scoped>
  23. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/904291
推荐阅读
相关标签
  

闽ICP备14008679号