AngularJS 的其他选项卡中的 sessionStorage 值变为空

sessionStorage value becomes null in other tab in AngularJS

登录后,我将会话中的值存储在 Angular:

sessionStorage.setItem("CustomerID", JSON.stringify(CustomerID));
sessionStorage.setItem("Email", JSON.stringify(email));

我在这里获取会话值:

var sessionvalue = sessionStorage.getItem("CustomerID");

在另一个选项卡中,如果我打开该页面,则 sessionvalue 变为 null。 它适用于同一个选项卡。

为什么在浏览器的其他标签中变成null

根据MDN

The sessionStorage property allows you to access a session Storage object for the current origin. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

来源: Window.sessionStorage


所以您可能需要其他类似 window.localStorage

的东西

示例:

// in a-page.html
localStorage.setItem("CustomerID", JSON.stringify(CustomerID));

// in b-page.html
var CustomerID =  localStorage.getItem("CustomerID", JSON.parse(CustomerID));