简体   繁体   English

更改window.location.href时如何还原状态

[英]How to restore state while changing window.location.href

After a user enters credentials on the login page, a post request is sent to the web server, which sends a cookie (if successful). 用户在登录页面上输入凭据后,发布请求将发送到Web服务器,该Web服务器将发送cookie(如果成功)。 On the client side, simultaneously, write the user details to my global redux store. 同时,在客户端,将用户详细信息写入我的全局redux存储。 On successful authentication from the server, I call window.location.href = 'newURL'. 从服务器成功通过身份验证后,我将调用window.location.href ='newURL'。 This call reloads my app. 此调用将重新加载我的应用程序。 Now the user info I stored to the global store is lost, and I am not able to display username which was enetred by the user. 现在,我存储到全局存储中的用户信息丢失了,并且我无法显示该用户使用的用户名。 How can I access the user credentials? 如何访问用户凭据? Thanks in advance :) 提前致谢 :)

As mentioned in comments. 如评论中所述。

You need to use either localStorage or sessionStorage to keep the data alive. 您需要使用localStorage或sessionStorage来保持数据活动。

For eg 例如

localStorage.setItem("user", {"name": "abc"});

Now, though you logged out your data will be there in localStorage and you get the data using 现在,尽管您注销了数据,但数据仍将存在于localStorage中,并且您可以使用

localStorage.getItem("user");

If you want to remove then 如果要删除,则

localStorage.removeItem("user") or window.localStorage.removeItem('user');

Same applies to sessionStorage as well 同样适用于sessionStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM