案例:
从a页面=>b页面=>c页面,当前在c页面,执行某方法后可以如同按了浏览器后退键一样返回b页面
方法:
①. 若项目使用vue-router,this.$router.go(-1)可以回到上一页
②. this.$router.back()
③. window.history.go(-1)
区别
①与②的区别是:
go(-1): 原页面表单中的内容会丢失:
1. this.$router.go(-1):后退+刷新;
2. this.$router.go(0):刷新;
3. this.$router.go(1):前进;
back(): 原页表表单中的内容会保留;
1. this.$router.back():后退 ;
2. this.$router.back(0):刷新;
3. this.$router.back(1):前进;
③的区别是:
history.go(-1)是返回浏览器的上一页,而由于Vue是单页面应用,有的浏览器对于hash变更不认为是两个不同的页面,在hash模式下就不会跳回浏览器上一页。
全部评论