简体   繁体   English

在不同的浏览器中打开页面时,令牌没有保存在$ window.sessionStorage中

[英]Token not getting saved in $window.sessionStorage when page is opened in different browser

I'm initializing the token that is generated after I log in to my application in $window.sessioStorage . 我正在初始化在$ window.sessioStorage中登录我的应用程序后生成的令牌。

var token = this.$window.sessionStorage["apiKey"];

But this token seem to be undefined when I copy the url(after log in ) and open in different browser.I'm redirected back to the login page. 但是当我复制url(登录后)并在不同的浏览器中打开时,这个标记似乎是未定义的。我被重定向回登录页面。 Does $window.sessionStorage holds the values of variables in the same browser window?Or what could be another way to retain the value of token even if I opened the page in different browser window? $ window.sessionStorage是否在同一浏览器窗口中保存变量的值?或者即使我在不​​同的浏览器窗口中打开页面,还有什么可以保留令牌的值?

That's the right behaviour 这是正确的行为

The sessionStorage property allows you to access a session Storage object. sessionStorage属性允许您访问会话存储对象。 sessionStorage is similar to 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. sessionStorage类似于localStorage,唯一的区别是当localStorage中存储的数据没有到期设置时,存储在sessionStorage中的数据在页面会话结束时被清除。 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. 在新选项卡或窗口中打开页面将导致启动新会话。

You can keep information even if the browser is closed and reopend another time by using localStorage , but, as I said, if you open other browser you'll have to set items agains. 即使浏览器关闭也可以保留信息,并使用localStorage重新打开另一个时间,但正如我所说,如果你打开其他浏览器,你将不得不再次设置项目。

Local storage examples here 本地存储的例子在这里

Code example : 代码示例:

// set something in localStorage
localStorage.setItem('bgcolor', 'green');


// get something from localStorage
localStorage.getItem('bgcolor') // it will output green

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

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