跨页面通讯

239 阅读1分钟

有时候我们希望在新的窗口登陆之后,之前打开的其他窗口也能同步登录状态。

这时候可以监听 “storage”事件,在登录成功之后,在storage里写入登录状态

然后其他窗口就能监听到这个storage事件

 window.addEventListener("storage", function(e) {    if (e.key === "user_data" && e.url.includes("login")) {      ......    }  });