vue项目中遇见了这样一个问题:使用this.$router.go(-1)回到上一页,路由改变了,但是页面展示还是停留在当前页面,需要手动刷新才能渲染跳转后的页面。

router-view 如下:

<router-view :key="$route.fullPath"></router-view>

一般情况用到上面这种写法就能解决问题了,但是我这里还是不生效。

然后我就想到了如下方法:

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

代码:

beforeRouteEnter(to, from, next) {
    next(()=> {
      window.sessionStorage.setItem('lasterRouter', from.path)
    })
  },
//返回上一页
this.$router.push(window.sessionStorage.getItem('lasterRouter'))
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐