this.$router.go(-1)无效

问题描述:h5页面放在android的webview中运行的时候,发现this.$router.go(-1)返回失效。

解决思路:在beforeRouteEnter钩子时,使用sessionStorage存储from.path;然后在点击返回的时候使用this.$router.push

代码:

beforeRouteEnter(to, from, next) {
    next(vm => {
      window.sessionStorage.setItem('lasterRouter', from.path)
    })
  },
//返回事件时调用
this.$router.push(window.sessionStorage.getItem('lasterRouter'))

另一种方法:直接调用浏览器的window.history.back()