vue点击实现跳转
路由跳转(Vue Router)
在Vue中实现页面跳转通常通过Vue Router完成,适用于单页应用(SPA)的场景。
声明式导航
<router-link to="/path">跳转到目标页</router-link>
编程式导航
// 基础跳转
this.$router.push('/path')
// 带参数跳转
this.$router.push({ path: '/user', query: { id: 123 } })
// 命名路由跳转
this.$router.push({ name: 'user', params: { id: 123 } })
替换当前路由(不保留历史记录)

this.$router.replace('/new-path')
原生跳转
当需要完全刷新页面或跳转到外部链接时,可以使用原生方法。
window.location
window.location.href = 'https://external.com'
a标签

<a href="/path" target="_blank">新窗口打开</a>
动态路径跳转
根据变量值进行动态跳转时:
const userId = 456
this.$router.push(`/user/${userId}`)
// 或使用对象形式
this.$router.push({
path: `/user/${userId}`
})
路由传参
通过params或query传递参数:
// params传参(需要路由配置占位符)
this.$router.push({ name: 'user', params: { id: 123 } })
// query传参(URL显示参数)
this.$router.push({ path: '/user', query: { id: 123 } })
路由守卫控制
在跳转前进行权限验证或条件判断:
router.beforeEach((to, from, next) => {
if (to.path === '/admin' && !isAdmin) {
next('/login')
} else {
next()
}
})
注意事项
- 使用Vue Router时确保已正确安装和配置路由
- 编程式导航通常在methods中调用
- 路径参数需要在路由配置中预先定义,例如
path: '/user/:id' - 查询参数会显示在URL中,而params参数不会(命名路由除外)






