PHP前端开发

vue怎么跳转路由

百变鹏仔 3周前 (09-25) #VUE
文章标签 跳转
在 vue.js 中,可使用以下方法跳转路由:router-link 组件:声明式跳转,通过 组件指定跳转目标。router.push/replace 方法:编程式跳转,通过 router.push 添加新的路由到历史记录中,或通过 router.replace 替换当前路由。其他方法:还可使用 router.go、router.back 和 router.forward 方法。

Vue 中跳转路由

在 Vue.js 中,使用 router-link 组件或 router.push/replace 方法来跳转路由。

router-link 组件

router-link 组件声明式跳转到另一个路由:

立即学习“前端免费学习笔记(深入)”;

<template><router-link to="/home">Home</router-link></template><script>// ...</script>

router.push/replace 方法

router.push 和 router.replace 方法编程式跳转到另一个路由。

用法:

import { useRouter } from 'vue-router'export default {  setup() {    const router = useRouter()    const goToHome = () =&gt; {      router.push('/home')    }    return {      goToHome    }  }}

其他方法

除了 router-link 和 router.push/replace 方法外,还有其他跳转路由的方法: