将用户对象保存在会话存储中 [英] Save user object in Session Storage

查看:130
本文介绍了将用户对象保存在会话存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 2和Typescript,并希望像全局变量一样保存用户对象,因此不必多次检索它.我找到了会话存储,现在将用户对象保存在那里.

I am using Angular 2 and Typescript and wanted to save the user object like a global variable so it hasn't to be retrieved multiple times. I found the session storage and now save the user object there.

您认为将其存储在其中是一种好习惯还是数据过于敏感?如果是这样,我还可以使用其他哪种缓存?

Do you think it is good practice to store it there or is the data too sensitve? If so, what other kind of cache could I use?

这是我现在使用的代码:

Here is the code I use right now:

user.service.ts:

user.service.ts:

getProfile() {
    let cached: any;
    if (cached = sessionStorage.getItem(this._baseUrl)) {
        return Observable.of(JSON.parse(cached));
    } else {
        return this.http.get(this._baseUrl).map((response: Response) => {
            sessionStorage.setItem(this._baseUrl, response.text());
            return response.json();
        });
    }
}

当ngOnInit()时在app.component中调用getProfile().应用程序的其他组件中也需要用户对象.

The getProfile() is called in the app.component when ngOnInit(). The user object is also needed in other components of the application.

推荐答案

在会话存储区中保存安全/敏感数据是可以的.

Its ok to have secure/sensitive data in session storage.

由于会话存储仅适用于当前表和域...

As session storage only available for current table and domain...

如果用户在另一个窗口选项卡中检查了相同的会话存储数据,那么它将不会在那里....因此其安全存储....

If user check same session storage data in another window tab then it will not be there....so its secure storage....

如果想了解更多信息,请查看 sessionStorage

If want to know more, please have look on sessionStorage

这篇关于将用户对象保存在会话存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆