我有一个本地存储变量来存储当前认证游戏的数据。这是我的应用程序的结构。本地存储问题 - 角2
打开游戏
我有具有存储在桌上游戏列表的表称为OpenGames。现在,当我点击表中的任何游戏(戒指)时,它会进行身份验证,并且已验证的游戏名称将存储在本地存储变量中并路由到显示页面(例如:显示游戏页面)当前存储的名称存储在本地存储中。
在第一个例子中,我认识到显示游戏页面上显示的是经过验证的游戏,但是当我回到OpenGames表来验证另一个游戏(Spiderman)时,本地存储仍然保留了第一个游戏我认证。在本地存储将存储蜘蛛侠之前,我将不得不重新验证蜘蛛侠。
检查记录的控制台,当我对游戏进行身份验证时,显示游戏页面上的本地存储显示旧的存储值,然后显示新的已验证游戏的详细信息。
我现在的问题是:如何确保在本地存储保存认证游戏之前进行认证?
验证
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)
}
更新验证
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;
}
});
}
设置本地存储之前检查它是否已经设置,如果它已经设置,然后删除本地存储并将新值设置为本地存储 –
@ArunKumaresh我真的可以与您的声明相关。如果你可以更加解释。但是如果我明白你的意思,当我从显示游戏页面返回到开放游戏桌以验证另一个游戏时,当我删除本地存储时,应用程序将抛出一个错误,指出自显示游戏以来'currentGame为null'页面打开之前认证 – XamarinDevil
你可以添加你试过的代码 –