我正在从当天的API中检索记录。如果为inventory_name = "Better luck next time!"
,我希望它推送到FailResultsPage
。否则,我希望它推送到ResultsPage
。无论我得到什么结果,我当前的代码始终将我定向到ResultsPage
。我在做什么错了?
提供商
getUserSpin(spin_date: string){
return this.http.get<GamesRecords>(`${this.dataApiUrl}//someapi &filters[spin_date]=${spin_date}`,
{ headers: new HttpHeaders().set('X-XSRF-TOKEN', this.getCookie('XSRF-TOKEN'))});}
.ts页面
export class GamesPage {
games: Games[];
inventory_name: string = 'Better luck next time!';
spin_date;
date;
gamed;
constructor(public navCtrl: NavController, public datepipe: DatePipe, private alertCtrl: AlertController, private christmasProvider: ChristmasProvider, private storage: Storage) {
gotoResultpage(){
this.date = new Date();
let spin_date = this.datepipe.transform(this.date, 'yyyy-MM-dd');
console.log(spin_date);
this.christmasProvider.getUserSpin(spin_date).pipe(
map((gamesResult: GamesRecords) => gamesResult && gamesResult.records)
).subscribe(gamed => {
this.games = gamed;
this.storage.set('inventory_name', gamed);
console.log(this.gamed);
});
if(this.gamed = this.inventory_name){
this.navCtrl.push(FailResultsPage);
console.log('push to fail page');
console.log(this.gamed);
}
else{
this.navCtrl.push(ResultsPage);
console.log('push to success page');
}
}
}
答案 0 :(得分:0)
由于它是一个异步调用,因此您不知道何时返回。因此,如果将else语句移到订阅函数中,请尝试以下代码
gotoResultpage(){
this.date = new Date(); let spin_date = this.datepipe.transform(this.date, 'yyyy-MM-dd'); console.log(spin_date); this.christmasProvider.getUserSpin(spin_date).pipe( map((gamesResult: GamesRecords) => gamesResult && gamesResult.records) ).subscribe(gamed => { this.games = gamed; this.storage.set('inventory_name', gamed); console.log(this.gamed); if(this.gamed == this.inventory_name){ this.navCtrl.push(FailResultsPage); console.log('push to fail page'); console.log(this.gamed); }else{ this.navCtrl.push(ResultsPage); console.log('push to success page'); } });
}