首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >局部存储问题-角2

局部存储问题-角2
EN

Stack Overflow用户
提问于 2017-05-07 09:11:22
回答 1查看 267关注 0票数 0

我有一个本地存储变量来存储当前经过身份验证的游戏的数据。这是我的应用程序的结构。

开放式游戏

我有一个名为OpenGames的表,它在表中存储了一个游戏列表。现在,当我单击表中的任何游戏(指环王)时,它进行身份验证,并将经过身份验证的游戏名称存储在本地存储变量中,并路由到一个页面(例如:显示游戏页),该页面在本地存储中显示当前存储的名称。

在第一个例子中,我意识到经过身份验证的游戏显示在显示游戏页面上,但是当我回到OpenGames表对另一个游戏(蜘蛛侠)进行身份验证时,本地存储仍然保留着我认证过的第一个游戏。在当地存储蜘蛛侠之前,我必须重新确认蜘蛛侠的身份。

检查登录控制台,当我对游戏进行身份验证时,显示游戏页上的本地存储在显示新的经过身份验证的游戏的详细信息之前显示旧的存储值。

我现在的问题是:如何确保在本地存储保存经过身份验证的游戏之前进行身份验证?

Auth

代码语言:javascript
复制
constructor(private httpService: Http) {
    let currentGame = JSON.parse(localStorage.getItem('currentGame'));    
}

authGame(game: string): Observable<boolean> {
    return this.http.post('http://localhost:9000/example.com',({game: game }))
           .map((response: Response) => {

        let game_name:string = response && response.json().game.name;

        if (game_name ) {
            // store name in variable 
            this.game_name = game_name;

            localStorage.setItem('currentGame', JSON.stringify({game_name:game_name })

            console('Current Game is ' +game_name)
            return true;
        } else {
            return false;
        }

    });      

}

显示游戏页面

代码语言:javascript
复制
constructor( private auth:AuthGame){
     this.currentGame = JSON.parse(localStorage.getItem('currentGame'));    
     this.currentGame.game_name);
     console('Current Game is ' currentGame.game_name)
}

更新Auth

代码语言:javascript
复制
constructor(private httpService: Http) {

    let currentGame = JSON.parse(localStorage.getItem('currentGame'));
}

authGame(game: string): Observable<boolean> {
    return this.http.post('http://localhost:9000/example.com',({game: game }))
            .map((response: Response) => {         
        let game_name:string = response && response.json().game.name;

        if (game_name ) {
            // store name in variable 
            this.game_name = game_name;
            if(localStorage.getItem('currentGame')) {
                localstorage.removeItem('currentGame');
                localStorage.setItem('currentGame', JSON.stringify({game_name:game_name }))   
                console('Current Game is ' +game_name)
            }
            return true;

        } else {
            return false;
        }

    });

}
EN

回答 1

Stack Overflow用户

发布于 2017-05-07 10:04:59

代码语言:javascript
复制
if(localStorage.getItem('currentGame'))
{
localStorage.removeItem('currentGame');
      localStorage.setItem('currentGame', JSON.stringify({game_name:game_name }))                    

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43829769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档