vue路由跳转方式

本文详细介绍了Vue中四种路由跳转方式(router-link, $router.push(), $router.replace() 和 $router.go()),包括带参数的使用技巧,并对比了它们的区别。

转自:https://www.cnblogs.com/miluluyo/p/11190648.html

vue路由跳转有四种方式

1. router-link

2. this.$router.push() (函数里面调用)

3. this.$router.replace() (用法同push)

4. this.$router.go(n)

一、不带参

1.1 router-link

<router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name  
//注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。

1.2 this.$router.push()

this.$router.push('/home') 
this.$router.push({name:'home'}) 
this.$router.push({path:'/home'})

1.3 this.$router.replace() (用法同push)

二、带参

2.1 router-link

<router-link :to="{name:'home', params: {id:1}}"> 
// params传参数 (类似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id" 
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
 
// html 取参  $route.params.id
// script 取参  this.$route.params.id
 
<router-link :to="{name:'home', query: {id:1}}"> 
 
// query传参数 (类似get,url后面会显示参数)
// 路由可不配置
 
// html 取参  $route.query.id
// script 取参  this.$route.query.id

2.2 this.$router.push(query传参 )

this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
 
// html 取参  $route.query.id
// script 取参  this.$route.query.id
 
params传参
 
this.$router.push({name:'home',params: {id:'1'}})  // 只能用 name
 
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
 
// html 取参  $route.params.id
// script 取参  this.$route.params.id

2.3 this.$router.replace() (用法同push)

this.$router.go(n)

this.router.go(n)this.router.go(n)this.router.go(n) 向前或者向后跳转n个页面,n可为正整数或负整数

区别:

this.$router.push 跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面

this.$router.replace 跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)

this.$router.go(n) 向前或者向后跳转n个页面,n可为正整数或负整数

Vue 路由跳转主要有以下几种常见方式: ### 使用 `<router-link>` 组件 `<router-link>` 是 Vue Router 提供的一个组件,用于创建导航链接。可以通过 `to` 属性指定要跳转路由地址或路由名称。 ```vue <template> <!-- 使用路径跳转 --> <router-link to="/home">Home</router-link> <!-- 使用路由名称跳转 --> <router-link :to="{ name: 'Home' }">Home</router-link> </template> ``` ### 编程式导航 #### `this.$router.push` `this.$router.push` 方法用于向历史记录中添加一个新的路由记录,实现页面跳转。可以传递路径字符串或包含路由信息的对象,还可以传递参数。 ```javascript // 传递路径字符串 this.$router.push('/menu'); // 传递包含路由名称和参数的对象 this.$router.push({ name: 'menuLink' }); this.$router.push({ name: "Home", params: { id: this.id } })[^2][^3]。 ``` #### `this.$router.replace` `this.$router.replace` 方法与 `push` 类似,但它不会向历史记录中添加新的路由记录,而是替换当前的路由记录,即无法通过浏览器的后退按钮回到上一个页面。 ```javascript // 传递路径字符串 this.$router.replace('/menu'); // 传递包含路由名称的对象 this.$router.replace({ name: "menuLink" })[^3]。 ``` #### `this.$router.go` `this.$router.go` 方法用于在历史记录中向前或向后跳转指定的步数。参数为正数向前跳转,为负数向后跳转。 ```javascript // 向后跳转一步 this.$router.go(-1); // 向前跳转一步 this.$router.go(1); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值