我有一个本地存储变量来存储当前经过身份验证的游戏的数据 . 这是我的应用程序的结构 .
Open Games
我有一个名为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;
}
});
}
Display Game Page
constructor( private auth:AuthGame){
this.currentGame = JSON.parse(localStorage.getItem('currentGame'));
this.currentGame.game_name);
console('Current Game is ' currentGame.game_name)
}
updated 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;
}
});
}
1 回答