在 vue.js 中,可以通过 组件或 router.push() 方法跳转页面。 组件用于创建可点击链接,而 router.push() 方法用于以编程方式跳转。此外,可以传递参数、使用 named route,以及使用 replace: true 替换历史记录。

Vue.js 页面跳转指南
如何使用 Vue.js 跳转到新页面?
在 Vue.js 中,可以通过 <router-link></router-link> 组件或 router.push() 方法进行页面跳转。
使用
立即学习“前端免费学习笔记(深入)”;
<router-link></router-link> 组件用于在 Vue.js 项目中创建可点击链接,当用户点击该链接时,它将触发页面跳转。
<code class="html"><template> <router-link to="/about">关于我们</router-link> </template></code>
使用 router.push() 方法
router.push() 方法用于以编程方式进行页面跳转。它接受一个路径或路由对象作为参数,并触发页面跳转。
<code class="js">this.$router.push('/about')</code>详细说明
-
使用 named route
可以使用 named route 来跳转到特定路由,例如:
<code class="html"><router-link :to="{ name: 'about' }">关于</router-link></code><code class="js">this.$router.push({ name: 'about' })</code> -
传递参数
可以使用
params或query属性传递参数到新页面:<code class="html"><router-link :to="{ path: '/user', params: { id: 123 } }">用户详情</router-link></code><code class="js">this.$router.push({ path: '/user', query: { id: 123 } })</code> -
使用 replace: true
如果希望替换历史记录中当前页面,可以使用
replace: true选项:<code class="js">this.$router.push({ path: '/about', replace: true })</code>










