【前端分享】Vue 页面跳转 + 返回并触发事件:Vue2 + Vue3 完整示例!

Vue 页面跳转+返回并触发事件:Vue2 + Vue3 完整示例

核心需求:

  1. A → B 页面跳转

  2. B 页面自动/手动返回A页面

  3. 返回A后,自动执行A页面的一个指定方法

我会用最常用、最简单的方案(路由传参 + 监听路由)实现,两套代码直接复制可用。


1. 一、Vue2 实现方案

1.1. 路由配置(公用)

router/index.js

复制代码
import Vuefrom'vue'
importRouterfrom'vue-router'
import A from'@/views/A.vue'
import B from'@/views/B.vue'

Vue.use(Router)
exportdefaultnewRouter({
routes: [
    { path: '/', name: 'A', component: A },
    { path: '/b', name: 'B', component: B }
  ]
})

1.2. A 页面(接收返回信号并执行方法)

views/A.vue

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

<script>
export default {
  name: 'PageA',
  methods: {
    // 跳转到B
    goToB() {
      this.$router.push('/b')
    },
    // B返回后需要执行的目标方法
    doSomethingAfterBack() {
      alert('✅ 从B页面返回A了,执行A页面方法!')
      console.log('A页面方法执行成功')
    }
  },
  // 监听路由:从B返回A时触发
  watch: {
    $route(to, from) {
      // 判断是否从 B 页面回来
      if (from.path === '/b') {
        this.doSomethingAfterBack()
      }
    }
  }
}
</script>

1.3. B 页面(返回A页面)

views/B.vue

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

<script>
export default {
  name: 'PageB',
  methods: {
    // 返回上一页(A页面)
    backToA() {
      this.$router.back()
      // 也可以用 this.$router.go(-1) 效果一样
    }
  },
  // 如果你想【进入B页面自动立刻返回A】,用这个钩子
  mounted() {
    // 自动返回A
    // this.$router.back()
  }
}
</script>

2. 二、Vue3 实现方案(Composition API)

2.1. 路由配置

router/index.js

复制代码
import { createRouter, createWebHistory } from'vue-router'
import A from'../views/A.vue'
import B from'../views/B.vue'

const routes = [
  { path: '/', name: 'A', component: A },
  { path: '/b', name: 'B', component: B }
]

const router = createRouter({
history: createWebHistory(),
  routes
})

exportdefault router

2.2. A 页面(Vue3 setup 语法糖)

views/A.vue

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

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

const router = useRouter()
const route = useRoute()

// 跳转到B
const goToB = () => {
  router.push('/b')
}

// B返回后要执行的方法
const doSomethingAfterBack = () => {
  alert('✅ Vue3:从B返回A,执行A方法成功!')
  console.log('A页面方法执行')
}

// 监听路由变化:从B回来就执行方法
watch(
  () => route.fullPath,
  (to, from) => {
    // 判断上一页是B
    if (document.referrer.includes('/b') || route.fullPath === '/' && from === '/b') {
      doSomethingAfterBack()
    }
  }
)
</script>

2.3. B 页面(Vue3 setup 语法糖)

views/B.vue

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

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()

// 返回A页面
const backToA = () => {
  router.back()
  // router.go(-1) 效果一样
}

// 如果需要【进入B自动返回A】
// onMounted(() => {
//   router.back()
// })
</script>

3. 三、进阶方案:精准控制(推荐生产环境)

如果需要精准控制 (不是所有从B返回都执行),可以用路由参数标记

3.1. A 页面跳转时

复制代码
// Vue2
this.$router.push({ path: '/b', query: { from: 'A' } })

// Vue3
router.push({ path: '/b', query: { from: 'A' } })

3.2. B 返回时

复制代码
// 带回标记
this.$router.back()

3.3. A 页面监听

复制代码
// Vue2
watch: {
  $route() {
    if (this.$route.query.from === 'A') {
      this.doSomething()
    }
  }
}

// Vue3
watch(() => route.query, (newVal) => {
  if (newVal.from === 'A') {
    doSomethingAfterBack()
  }
})

3.4. 总结

  1. 核心原理:监听路由变化 → 判断来源页面 → 执行方法

  2. Vue2watch: $route

  3. Vue3watch(route)

  4. 返回方法$router.back() / $router.go(-1)

  5. 精准触发 :路由 query 传标记