现在每个页面的左上角有一个返回按钮<
点击时的代码是this.$router.back(-1),返回上一个路由
但是我们现在有这样一个需求,把其中某一页分享出去,用户打开时并没有上一条路由的历史记录,所以点击<按钮时没有反应。
所以应该怎么判断有没有上一条路由的历史记录。
代码:
routerback: function () {
this.$router.back(-1)
},
- 8月17日提问
- 评论
- 邀请回答
- 编辑
3个回答
答案对人有帮助,有参考价值0答案没帮助,是错误的答案,答非所问
已采纳
在页面一开始加上一个全局的函数:
activated: function () {
this.$setgoindex()
}
这个函数是这样的,判断当前页面的历史记录是不是小于等于1,如果小于等于1,说明这个页面没有可以返回的上一页,如果没有可以返回的上一页,就给地址栏加上一个goindex=true的参数,这样你从这个页面在往下一个页面跳转在返回,这个参数就一直加上的
Vue.prototype.$setgoindex = function () {
if (window.history.length <= 1) {
if (location.href.indexOf('?') === -1) {
window.location.href = location.href + '?goindex=true'
} else if (location.href.indexOf('?') !== -1 && location.href.indexOf('goindex') === -1) {
window.location.href = location.href + '&goindex=true'
}
}
}
然后在左上角返回按钮加上这个逻辑:
if (this.$route.query.goindex === 'true') {
this.$router.push('/')
} else {
this.$router.back(-1)
}
这样就可以了