原文: https://segmentfault.com/q/1010000010714863segmentfault
如今每一个页面的左上角有一个返回按钮<
点击时的代码是this.$router.back(-1),返回上一个路由
可是咱们如今有这样一个需求,把其中某一页分享出去,用户打开时并无上一条路由的历史记录,因此点击<按钮时没有反应。
因此应该怎么判断有没有上一条路由的历史记录。
代码:
routerback: function () {
this.$router.back(-1)
},
this
window.history.length是整个记录数量,包括前进,不排除出现当前页不是最后一条记录spa
methods: {
back(){
if (window.history.length <= 1) { this.$router.push({path:'/'}) return false } else { this.$router.go(-1) } //上面都没执行就说明卡在当前页不是最后一条, histroy记录数量大于1,又没有回退记录,只能返回首页, //若是上面都执行了 页面都跳走了,这个也就不用管了 setTimeout(() => { this.$router.push({path:'/'}) },500) } },