网页的window.location.replace方法 转跳后上一个链接依然存在history中?

2022/07/05
提问

企业微信中的网页使用 授权登入需要转跳到一个新链接,但是使用window.location.replace转跳,上一个页面依然会存在history中,导致点击返回时,会回到上一个页面

回答·2
2023/10/16

上个月我也遇到这样的情况,首次登录授权完成, replace到业务页面, 还有返回, 看了历史记录, 授权完成到首页, 有4~5条历史记录(我们是封装的Oauth包,一般记录应该没这么多), replace没生效,还有历史记录,


bug是Oauth页面是封装的h5,登录完成到首页后, 还能返回OauthH5页面, 然后页面再跳转回首页, 导致按返回一直在在授权和首页鬼畜跳转, 我向上反映说微信浏览器replace不行, 我们组长就接手了,我也不清楚他的解决方式行不行,你们可以试下,行的话告诉我下


我们技术栈是vue


router-link添加 replace

        <router-link

          class="link"

          replace

          :to="{name: 'login', query: {}) }"

        >


路由守卫添加router.beforeEach next函数添加replace:true


router.beforeEach((to, from, next) => {

    if (from.name && !to.query.appId) {

      if (from.query.appId && from.params.id) {

        next({

          replace: true,

          name: to.name,

          query: {

            ...to.query

          },

          params: {

            ...to.params

          }

        })

      } else {

        next()

      }

    } else {

      next()

    }

})


其他需要使用location.replace的地方正常用replace


具体好不好用,得你们去试才行

赞同
评论
2023/08/30

这个问题,有解决方案了吗。

赞同
评论