Home…">
当前位置:首页 > VUE

vue实现跳转的方式

2026-02-22 10:52:55VUE

vue-router 的基本跳转方式

使用 router-link 组件进行声明式导航

<router-link to="/home">Home</router-link>
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>

在脚本中使用编程式导航

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

路由传参的三种方式

路径参数传参(需在路由配置中定义)

// 路由配置
{ path: '/user/:id', component: User }

// 跳转方式
this.$router.push('/user/123')

query 参数传参

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

props 传参(路由配置中启用)

// 路由配置
{ path: '/user/:id', component: User, props: true }

// 组件中接收
props: ['id']

路由跳转的其他方法

替换当前路由(不记录历史)

this.$router.replace('/login')

前进后退导航

vue实现跳转的方式

this.$router.go(1)  // 前进
this.$router.go(-1) // 后退

命名路由和命名视图的使用

命名路由跳转

// 路由配置
{ path: '/user/:id', name: 'user', component: User }

// 跳转方式
this.$router.push({ name: 'user', params: { id: 123 } })

命名视图配置(多组件布局)

<router-view name="header"></router-view>
<router-view></router-view>
<router-view name="footer"></router-view>

导航守卫控制跳转

全局前置守卫

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    // 需要登录验证
  } else {
    next()
  }
})

组件内守卫

vue实现跳转的方式

beforeRouteEnter(to, from, next) {
  // 组件实例尚未创建
  next(vm => {
    // 通过vm访问组件实例
  })
}

动态路由跳转

添加动态路由

router.addRoute({ path: '/new', component: NewComponent })

跳转到动态路由

this.$router.push('/new')

路由懒加载优化

使用动态导入实现懒加载

const User = () => import('./views/User.vue')

路由配置中使用

{ path: '/user', component: User }

标签: 跳转方式
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

php怎样实现页面跳转页面跳转页面

php怎样实现页面跳转页面跳转页面

PHP页面跳转的实现方法 在PHP中实现页面跳转有多种方式,以下是常见的几种方法: header函数跳转 header("Location: target_page.php"); exit();…

vue实现音乐跳转

vue实现音乐跳转

使用 Vue 实现音乐跳转功能 在 Vue 中实现音乐跳转功能,通常需要结合 HTML5 的 <audio> 元素和 Vue 的数据绑定及事件处理机制。以下是实现音乐跳转的几种常见方法。…

vue怎么实现跳转

vue怎么实现跳转

Vue 实现页面跳转的方法 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于声明式导航。通过 to 属性指定目标路由路径或命名路由。 <r…

vue实现自动跳转

vue实现自动跳转

vue实现自动跳转的方法 在Vue中实现自动跳转功能可以通过多种方式实现,以下是几种常见的方法: 使用vue-router的push方法 通过vue-router的this.$router.push…