当前位置:首页 > VUE

vue 实现跳转

2026-03-07 10:56:36VUE

路由跳转实现

在Vue中实现页面跳转通常使用Vue Router,这是Vue.js官方的路由管理器。它和Vue.js核心深度集成,使构建单页面应用变得易如反掌。

安装Vue Router:

npm install vue-router

配置基本路由:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = new VueRouter({
  routes
})

export default router

声明式导航

使用<router-link>组件创建导航链接:

<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>

可以添加active-class属性自定义激活状态的类名:

<router-link to="/about" active-class="active-link">About</router-link>

编程式导航

通过this.$router.push()方法实现编程式导航:

methods: {
  goToAbout() {
    this.$router.push('/about')
  }
}

带参数的跳转:

this.$router.push({ path: '/user', query: { id: '123' } })
// 或
this.$router.push({ name: 'user', params: { id: '123' } })

命名路由

在路由配置中给路由设置名称:

const routes = [
  {
    path: '/user/:id',
    name: 'user',
    component: User
  }
]

使用命名路由跳转:

this.$router.push({ name: 'user', params: { id: '123' } })

路由传参

通过params传递参数:

this.$router.push({ name: 'user', params: { userId: '123' } })

在目标组件中接收参数:

this.$route.params.userId

通过query传递参数:

this.$router.push({ path: '/user', query: { id: '123' } })

在目标组件中接收query参数:

this.$route.query.id

路由守卫

全局前置守卫:

vue 实现跳转

router.beforeEach((to, from, next) => {
  // 必须调用next()
})

路由独享守卫:

const routes = [
  {
    path: '/admin',
    component: Admin,
    beforeEnter: (to, from, next) => {
      // 守卫逻辑
    }
  }
]

组件内守卫:

export default {
  beforeRouteEnter(to, from, next) {
    // 不能访问this
  },
  beforeRouteUpdate(to, from, next) {
    // 当前路由改变但组件被复用时调用
  },
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件时调用
  }
}

动态路由匹配

使用冒号标记动态路径参数:

const routes = [
  { path: '/user/:id', component: User }
]

在组件中访问参数:

this.$route.params.id

响应路由参数变化:

watch: {
  '$route'(to, from) {
    // 对路由变化作出响应
  }
}

路由模式

hash模式(默认):

const router = new VueRouter({
  mode: 'hash',
  routes
})

history模式(需要服务器配置支持):

vue 实现跳转

const router = new VueRouter({
  mode: 'history',
  routes
})

路由懒加载

使用动态导入实现路由懒加载:

const routes = [
  {
    path: '/about',
    component: () => import('./views/About.vue')
  }
]

嵌套路由

定义嵌套路由:

const routes = [
  {
    path: '/user',
    component: User,
    children: [
      {
        path: 'profile',
        component: UserProfile
      },
      {
        path: 'posts',
        component: UserPosts
      }
    ]
  }
]

在父路由组件中添加<router-view>

<div class="user">
  <router-view></router-view>
</div>

路由重定向和别名

重定向:

const routes = [
  { path: '/home', redirect: '/' }
]

命名重定向:

const routes = [
  { path: '/home', redirect: { name: 'homepage' } }
]

路由别名:

const routes = [
  { path: '/', component: Home, alias: '/home' }
]

导航守卫中的元字段

定义路由元信息:

const routes = [
  {
    path: '/admin',
    meta: { requiresAuth: true }
  }
]

在守卫中使用:

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 需要认证
  }
})

标签: 跳转vue
分享给朋友:

相关文章

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现layout

vue实现layout

Vue 实现 Layout 布局的方法 在 Vue 中实现 Layout 布局通常涉及路由嵌套、组件化设计和动态渲染。以下是几种常见的实现方式: 使用嵌套路由 通过 Vue Router 的嵌套路由…

vue  实现关联属性

vue 实现关联属性

Vue 实现关联属性 在 Vue 中实现关联属性通常涉及计算属性(computed properties)或侦听器(watchers),用于根据其他属性的变化动态更新目标属性。 使用计算属性 计算属…

vue如何实现计算

vue如何实现计算

Vue 实现计算的方法 Vue 提供了多种方式来实现计算逻辑,包括计算属性、方法、侦听器等。以下是几种常见的实现方式: 计算属性(Computed Properties) 计算属性是基于它们的响应式…