【前端分享】vue开发项目,如何实现 从A页面跳转到B页面,并传值!

Vue 页面双向跳转+传值(A↔B)完整实现

在 Vue 中实现 A → B → A 并携带参数回传 ,核心用 Vue Router 配合路由传参 ,最常用两种方案:编程式导航 + query/params 传参 (简单场景)、路由守卫 + 状态管理(复杂场景)。

我直接给你最实用、开箱即用的实现方法,分步骤写清楚。


一、准备工作(路由配置)

先确保你的 router/index.js 配置好 A、B 页面路由:

复制代码
import Vuefrom'vue'
importRouterfrom'vue-router'
importPageAfrom'@/views/PageA'
importPageBfrom'@/views/PageB'

Vue.use(Router)

exportdefaultnewRouter({
routes: [
    { path: '/', name: 'PageA', component: PageA },
    { path: '/pageB', name: 'PageB', component: PageB }
  ]
})

二、核心实现:A → B → A 回传参数

方案1:最简单 ------ 路由 query 传参(推荐新手)

适用:简单字符串、数字等小数据,URL 会显示参数(无隐私)。

步骤1:A 页面跳转到 B 页面

PageA.vue(发送方)

复制代码
<template>
  <div>
    <h2>A 页面</h2>
    <button @click="goToB">跳转到 B 页面</button>
  </div>
</template>

<script>
export default {
  methods: {
    goToB() {
      // 跳转到 B 页面,可携带参数给 B
      this.$router.push({
        path: '/pageB',
        query: { from: 'A页面' } // 传给 B 的参数
      })
    }
  }
}
</script>

步骤2:B 页面接收参数,再跳回 A 并传值

PageB.vue(中转+回传)

复制代码
<template>
  <div>
    <h2>B 页面</h2>
    <p>来自 A 的参数:{{ $route.query.from }}</p>
    <button @click="backToA">跳回 A 页面,并传递消息</button>
  </div>
</template>

<script>
export default {
  methods: {
    backToA() {
      // 跳回 A,**携带回传参数**
      this.$router.push({
        path: '/',
        query: { 
          msg: '我是B页面传回的消息',
          status: 'success' 
        }
      })
    }
  }
}
</script>

步骤3:A 页面接收 B 传回的消息

PageA.vue(接收回传参数)

复制代码
<script>
export default {
  // 监听路由变化(必须加,否则页面不刷新拿不到参数)
  watch: {
    $route(to) {
      this.getMsgFromB(to.query)
    }
  },
  mounted() {
    // 初始化也获取一次
    this.getMsgFromB(this.$route.query)
  },
  methods: {
    getMsgFromB(query) {
      if (query.msg) {
        console.log('B 传回的消息:', query.msg)
        alert(query.msg) // 你可以渲染到页面、存储到变量等
      }
    }
  }
}
</script>

方案2:隐私数据 ------ params 传参(URL 不显示)

适用 :不想让参数暴露在 URL 中,必须用路由 name 跳转。

A 跳 B
复制代码
this.$router.push({
  name: 'PageB', // 必须用 name
  params: { id: 1001 }
})
B 跳回 A 并传参
复制代码
this.$router.push({
  name: 'PageA',
  params: { msg: 'B 传回的隐私消息' }
})
A 接收 params
复制代码
this.$route.params.msg

方案3:复杂数据 ------ 全局状态管理(Vuex/Pinia)

适用:传递对象、数组、大量数据,或多个页面共享数据。

存数据(B 页面)
复制代码
// Vuex
this.$store.commit('SET_MSG', 'B传给A的数据')

// Pinia
this.store.msg = 'B传给A的数据'
跳回 A
复制代码
this.$router.push('/')
A 页面取数据
复制代码
computed: {
  msgFromB() {
    // Vuex
    return this.$store.state.msg
    // Pinia
    // return this.store.msg
  }
}

三、关键知识点(必看)

    1. $router 和 $route 的区别
    • • $router:路由实例,用来跳转页面 (push/go/back)

    • • $route:当前路由信息,用来获取参数 (query/params)

    1. query 和 params 区别
    方式 URL 显示 数据类型 刷新页面
    query 显示(?msg=xxx) 字符串 参数保留
    params 不显示 任意 参数丢失
    1. A 页面必须监听 $route

    因为 Vue 页面复用,不监听路由变化,无法实时拿到回传参数。


四、最简完整代码(直接复制可用)

PageA.vue

复制代码
<template>
  <div>
    <h1>A 页面</h1>
    <p>{{ msg }}</p>
    <button @click="goToB">去 B 页面</button>
  </div>
</template>

<script>
export default {
  data() {
    return { msg: '' }
  },
  watch: { $route: 'getMsg' },
  mounted() { this.getMsg() },
  methods: {
    goToB() { this.$router.push('/pageB') },
    getMsg() {
      if (this.$route.query.msg) {
        this.msg = this.$route.query.msg
      }
    }
  }
}
</script>

PageB.vue

复制代码
<template>
  <div>
    <h1>B 页面</h1>
    <button @click="backToA">返回 A 并传消息</button>
  </div>
</template>

<script>
export default {
  methods: {
    backToA() {
      this.$router.push({
        path: '/',
        query: { msg: '✅ 我是从B页面带回来的消息' }
      })
    }
  }
}
</script>

总结

    1. 简单数据 → 用 query 传参(最方便、永不丢失)
    1. 隐私数据 → 用 params 传参
    1. 复杂对象 → 用 Vuex/Pinia
    1. A 页面必须监听 $route 才能实时获取回传参数
相关推荐
Dream-Y.ocean1 小时前
鸿蒙PC平台宅匠 3D 室内设计适配实战:基于 Electron 壳方案的 Three.js 双渲染引擎与参数化户型编辑器开发
javascript·3d·harmonyos
Csvn2 小时前
性能监控
前端
Highcharts.js2 小时前
Highcharts Gantt使用符号的自定义数据标签开发甘特图示例
javascript·甘特图·highcharts·图表开发·实战代码·响应式图表
Csvn2 小时前
构建工具
前端
FRing033 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
Java小卷4 小时前
JuanForm vs FormCreate!Vue3 表单引擎联动配置 PK!
vue.js·低代码
kyriewen5 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒5 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫6 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus6 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude