vue点击按钮实现跳转
实现 Vue 按钮跳转的方法
在 Vue 中实现按钮点击跳转可以通过多种方式完成,以下是几种常见的方法:
使用 router-link 组件
适用于声明式导航,适合模板中使用:
<router-link to="/target-path">
<button>跳转到目标页</button>
</router-link>
使用编程式导航
通过 $router.push 方法实现:

<button @click="$router.push('/target-path')">跳转到目标页</button>
或者使用方法封装:
<button @click="navigateToPage">跳转到目标页</button>
<script>
export default {
methods: {
navigateToPage() {
this.$router.push('/target-path');
}
}
}
</script>
带参数的跳转
传递路由参数:

<button @click="$router.push({ path: '/user', query: { id: 123 } })">
带查询参数跳转
</button>
或者使用命名路由:
<button @click="$router.push({ name: 'user', params: { userId: 123 } })">
带参数跳转
</button>
在新标签页打开
使用 router.resolve 方法:
<button @click="openNewTab">新窗口打开</button>
<script>
export default {
methods: {
openNewTab() {
const route = this.$router.resolve({ path: '/target-path' });
window.open(route.href, '_blank');
}
}
}
</script>
注意事项
- 确保项目已安装并配置了
vue-router - 路径需要与路由配置中的路径匹配
- 使用编程式导航时,
this.$router必须在 Vue 实例上下文中可用 - 对于带参数的跳转,目标路由需要预先定义好参数接收方式
这些方法覆盖了 Vue 中按钮跳转的常见场景,可以根据具体需求选择合适的方式。






