我有一个本地存储变量来存储当前经过身份验证的游戏的数据。这是我的应用程序的结构。
开放式游戏
我有一个名为OpenGames的表,它在表中存储了一个游戏列表。现在,当我单击表中的任何游戏(指环王)时,它进行身份验证,并将经过身份验证的游戏名称存储在本地存储变量中,并路由到一个页面(例如:显示游戏页),该页面在本地存储中显示当前存储的名称。
在第一个例子中,我意识到经过身份验证的游戏显示在显示游戏页面上,但是当我回到OpenGames表对另一个游戏(蜘蛛侠)进行身份验证时,本地存储仍然保留着我认证过的第一个游戏。在当地存储蜘蛛侠之前,我必须重新确认蜘蛛侠的身份。
检查登录控制台,当我对游戏进行身份验证时,显示游戏页上的本地存储在显示新的经过身份验证的游戏的详细信息之前显示旧的存储值。
我现在的问题是:如何确保在本地存储保存经过身份验证的游戏之前进行身份验证?
Auth
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;
}
});
}显示游戏页面
constructor( private auth:AuthGame){
this.currentGame = JSON.parse(localStorage.getItem('currentGame'));
this.currentGame.game_name);
console('Current Game is ' currentGame.game_name)
}更新Auth
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;
}
});
}发布于 2017-05-07 10:04:59
if(localStorage.getItem('currentGame'))
{
localStorage.removeItem('currentGame');
localStorage.setItem('currentGame', JSON.stringify({game_name:game_name }))
}https://stackoverflow.com/questions/43829769
复制相似问题