vue路由跳转的所有方法
时间: 2023-11-21 22:13:04 浏览: 266
Vue 路由有以下几种跳转方法:
- 声明式导航:通过
<router-link>
标签在模板中声明式地导航。
<router-link to="/path">Go to Path</router-link>
- 编程式导航:在 JavaScript 代码中通过路由实例的方法进行跳转。
// 带参数的路由跳转
this.$router.push({ path: '/path', query: { foo: 'bar' } })
// 命名路由跳转
this.$router.push({ name: 'user', params: { userId: 123 } })
// 返回上一页
this.$router.go(-1)
- 导航守卫:通过在路由配置中设置导航守卫函数,控制路由跳转的行为。
const router = new VueRouter({
routes: [
{
path: '/path',
component: MyComponent,
beforeEnter: (to, from, next) => {
// 在跳转到 '/path' 前执行的逻辑
next()
}
}
]
})
- 路由重定向:在路由配置中设置重定向规则,将路由导向到指定的页面。
const router = new VueRouter({
routes: [
{
path: '/',
redirect: '/home'
}
]
})
相关推荐















