【vue】中this.$router.back()、this.$router.go()的区别使用

1,539 阅读1分钟

blog.csdn.net/weixin\_459…

解析

开发中遇到返回上一页的需求,有两个方法可选:

1.$router.back()

2.$router.go()

在使用中发现,使用r o u t e r . b a c k ( ) 和 router.back()和router.back()和router.go(-1)作用相同,都是返回原页面。但如果原页面路由携带参数,使用以上两个方法返回的原页面路由参数消失,此时使用$router.back(-1)返回原页面路由参数仍存在。

代码

go(-1): 原页面表单中的内容会丢失;
this.$router.go(-1):后退+刷新;
this.$router.go(0):刷新;
this.$router.go(1) :前进
 
back(): 原页表表单中的内容会保留;
this.$router.back():后退 ;
this.$router.back(0) 刷新;
this.$router.back(1):前进