java小菜一枚 2024-05-29 11:45 采纳率: 89.1%
浏览 3

router.push有时候在Safari浏览器中无反应

router.push有时候在Safari浏览器中无反应,也就是停留在当前页面,我的代码是这样的,应该没有什么问题吧。

loginInterface
    .logout()
    .then(res => {
      if (res.code === 1000) {
        localStorage.removeItem("user-info");
        Cookies.remove("Authorization");
        Cookies.remove("multiple-tabs");
        router.push("/login").then(() => {
          message("退出登录成功!", { customClass: "el", type: "success" });
        });
      } else {
        message(res.message, { customClass: "el", type: "error" });
      }
    })
    .catch(error => {
      message(error.response.data.message, {
        customClass: "el",
        type: "error"
      });
    });

请求完成之后执行跳转登录页面,但是有时候会失效。我查看了网上的解决办法说可能是因为Safari的缓存机制的问题,有没有办法从代码中彻底解决呢?
就如图这样:

img

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-29 11:45
    关注

    引自免费微信小程序:皆我百晓生

    在Safari浏览器中,router.push有时可能会因为缓存机制导致页面不刷新。为了解决这个问题,你可以尝试在调用 router.push 的时候添加一个查询字符串参数,这个参数可以是一个时间戳,确保每次调用都是唯一的。这将强制浏览器加载新的页面而不是从缓存中获取。

    以下是修改后的代码示例:

    // 获取当前的时间戳作为唯一标识
    const timestamp = new Date().getTime();
    
    loginInterface
        .logout()
        .then(res => {
          if (res.code === 1000) {
            localStorage.removeItem("user-info");
            Cookies.remove("Authorization");
            Cookies.remove("multiple-tabs");
    
            // 添加时间戳到跳转路径
            const loginPathWithTimestamp = "/login?" + timestamp;
            router.push(loginPathWithTimestamp).then(() => {
              message("退出登录成功!", { customClass: "el", type: "success" });
            });
          } else {
            message(res.message, { customClass: "el", type: "error" });
          }
        })
        .catch(error => {
          message(error.response.data.message, {
            customClass: "el",
            type: "error"
          });
        });
    

    这种方法可以有效地防止Safari浏览器因缓存问题而不会刷新页面。但需要注意的是,虽然这种方法通常有效,但并不是所有情况下都能解决缓存问题,因为它依赖于浏览器的行为。如果你仍然遇到问题,可能需要进一步检查其他可能导致问题的因素,比如路由守卫、异步操作等。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月29日

悬赏问题

  • ¥15 adv找不到可向其添加属性的有效项目。
  • ¥15 cesm2.2.0移植问题。./manage_externals/checkout_externals
  • ¥35 pcb上电后,不一会儿,主控过热烧毁
  • ¥15 ubuntu,windows,文件传输,Linux
  • ¥15 北斗定位,cors,无人机,图传
  • ¥15 Unity热更新框架问题
  • ¥20 如何提交icra的附录.应该在哪里提交
  • ¥20 请教yolov8出现如下bug怎么办
  • ¥30 Armoury Crate 无法打开链接然后卸载了无法安装回去
  • ¥15 压力传感器控制led