Vue.js 提供四种跳转链接的方式:使用 <router-link> 组件;使用 $router.push() 方法;使用 $router.replace() 方法;使用 window.location.href 属性。

如何在 Vue 中跳转链接
Vue.js 提供了多种方法来跳转链接,下面是最常用的方式:
1. 使用 <router-link> 组件
<router-link> 组件是 Vue Router 提供的组件,它允许在单页面应用程序中使用路由导航。
立即学习“前端免费学习笔记(深入)”;
<code class="html"><router-link to="/home">Home</router-link></code>
2. 使用 $router.push() 方法
$router.push() 方法将新的 URL 推入历史记录堆栈,从而触发导航。
<code class="javascript">this.$router.push('/home');</code>3. 使用 $router.replace() 方法
$router.replace() 方法与 $router.push() 类似,但它不会在历史记录中创建新的条目,而是替换当前 URL。
<code class="javascript">this.$router.replace('/home');</code>4. 使用 window.location.href
window.location.href 属性可以用来手动设置浏览器的 URL。
<code class="javascript">window.location.href = '/home';</code>
其他注意事项:
- 对于动态链接,可以使用
:to属性来绑定 Vue 数据。 - 可以使用
target属性来指定链接在新窗口或选项卡中打开。 - 可以使用
event.preventDefault()来阻止链接的默认行为。








