详细总结this.$router.push与this.$router.replace

本文详细介绍了Vue.js中this.$router.push和this.$router.replace的方法及应用,包括声明式和编程式的路由跳转方式,并对比了这两种方法的区别,特别是关于路由历史记录的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、this.$router.push

1.1 使用方法

1.声明式
< router-link :to="…">:当使用router-link进行路由跳转时,实际上是调用了router.push()进行路由跳转
2.编程式 :this.$router.push(’路径‘)

1.2 使用语句

1.字符串
this.$router.push(‘index’)

2.对象
this.$router.push({path:‘index’})
3.携带参数
3.1 path 跳转方式,只可以用query传参

this.$router.push( {path:‘login’, query: { userName: ‘01testuser2’}})

3.2 name 跳转方式,可以用query和params传参
this.$router.push( {name:‘login’, query: { userName: ‘01testuser2’}})

this.$router.push( {name:‘login’, params: { userName: ‘01testuser2’}})
3.3query传参与params传参区别:query传参相当于get请求,在浏览器的url地址中会显示参数;params相当于post请求,在浏览器的地址栏中不显示。

二、this.$router.replace

this.router.replace的使用与this.router.replace的使用与this.router.replace使this.router.push相同,区别就是通过this.router.push()进行路由跳转,会在路由栈中添加一个记录,点击浏览器的返回时会跳转到上一个页面。而this.router.push()进行路由跳转,会在路由栈中添加一个记录,点击浏览器的返回时会跳转到上一个页面。而this.router.push()this.router.replace不会在路由栈中添加记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值