Я пытаюсь сделать приложение-викторину. В этом приложении вопросы будут приходить одно за другим. когда выбираю ответ, новых вопросов нет. Но это происходит, когда я обновляю sh страницу. как я могу решить эту проблему?
api.service.ts
getQuestion() {
return this._http.get(this.getUrl).subscribe(data => {
for (var i = 0; i < data['results'].length - 1; i++) {
var random = Math.floor(Math.random() * 3)
var correctAnswer = data['results'][i].correct_answer;
//We put the correct answer in a random index
var tmp = data['results'][i]['incorrect_answers'][random]
data['results'][i]['incorrect_answers'][random] = correctAnswer
data['results'][i]['incorrect_answers'].push(tmp)
localStorage.setItem('questions', JSON.stringify(data))
// console.log(this.index)
// console.log(data['results'][this.index])
//var a = this.question[this.index]
// console.log(data['results'].length)
}
},
err => console.error(err)
)
}
question.component.ts
VerifyAnswer(o, e) {
if (o === this.correctAnswer) {
this.score = this.score + 10
}
if (this.index == 9) {
// when the last Question
localStorage.setItem('score', this.score.toString())
} else {
// navigate to next Question
this._router.navigate(['question/' + (this.index + 1)]);
}
}
getQuestion() {
this.index = parseInt(this._route.snapshot.paramMap.get('id'));
if (localStorage.getItem('q') !== null) {
var data = JSON.parse(localStorage.getItem('questions'))
this.q = data.results[this.index]
this.correctAnswer = this.q.correct_answer
console.log(this.q.correct_answer)
console.log(this.score)
} else {
this._apiService.getQuestion()
var data = JSON.parse(localStorage.getItem('questions'))
this.q = data.results[this.index]
}
}
router
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'question/:id', component: QuestionComponent },
];