Vue.js 页面跳转有两种方式:路由跳转,适合复杂页面导航,使用 vue-router 管理,需要安装、创建路由表,并通过 router.push() 进行跳转。手动跳转,适合简单页面导航,可使用 window.location.assign()、this.$router.go() 或 window.open(),无需使用 vue-router,但要注意不会触发 Vue 路由钩子。

Vue 页面跳转详解
Vue.js 中页面跳转有两种主要方式:
一、路由跳转
路由跳转是一种更优雅、更灵活的跳转方式,适用于需要管理复杂页面导航的单页面应用程序 (SPA)。
立即学习“前端免费学习笔记(深入)”;
- 安装 vue-router
npm install vue-router
- 创建路由表
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = new VueRouter({
routes
})- 使用路由跳转
二、手动跳转
手动跳转适合简单页面导航或特殊情况。
- 使用
window.location.assign()
window.location.assign('https://example.com')- 使用
this.$router.go()
this.$router.go(1) // 返回 this.$router.go(-1) // 前进
- 使用
window.open()
window.open('https://example.com', '_blank') // 在新窗口中打开注意事项:
- 路由跳转会触发 Vue 实例的
beforeRouteEnter、beforeRouteLeave和beforeRouteUpdate钩子。 - 手动跳转不会触发 Vue 路由钩子。
- 手动跳转时,使用
pushState避免历史记录中出现丑陋的 URL。










