I am trying to create a quiz using vue.js and cannot seem to figure out how to make the "Next" button iterate through my data. The hope is for the screen to display the object containing the question with answers (i.e. questionOne, questionTwo), then show the next object when "Next" is clicked. I have several attempts in the existing code as you will see, but none work.
Quiz Component Template:
<template>
<div class="quiz-container">
<div
class="question-container">
<h1> {{ currentQuestion.q }} </h1>
</div>
<div class="answer-container">
<v-btn
v-for="answer in currentQuestion.ans"
:key="answer"
outlined
block
x-large
class="answer-btn"
>
{{ answer }}
</v-btn>
</div>
<div
class="navigation flex-row"
>
<v-btn text x-large @click="questionNav.curr--">Back</v-btn>
<v-spacer />
<v-btn text x-large @click="qNext()">Next</v-btn>
</div>
</div>
</template>
Quiz Script:
<script>
import { mapGetters } from 'vuex';
export default {
name: 'quiz',
computed: {
...mapGetters('user', {
loggedIn: 'loggedIn'
})
},
data: () => ({
curr: 0,
currentQuestion: {
q: 'kasjdn' ,
ans: ['1', '2', '3']
},
questionOne: {
q: 'How many minutes do you want to spend?' ,
ans: ['Short (15-20)', 'Medium (20-40)', 'Long (40-60)']
},
questionTwo: {
q: 'What muscle group do you want to focus on?' ,
ans: ['Upper Body', 'Lower Body', 'Core', 'Full Body']
},
questionThree: {
q: 'What level intensity do you want?' ,
ans: ['Leisure Walking', 'Speed Walking', 'Jogging', 'Sprinting']
},
questionParts: [this.questionOne, this.questionTwo, this.questionThree]
}),
methods: {
questionNav: function () {
questionParts = [this.questionOne, this.questionTwo, this.questionThree]
currentQuestion = questionParts[curr]
},
qNext: function () {
this.currentQuestion = this.questionParts[this.curr++]
}
}
}
</script>
As you can see, I have tried a "qNext" method and a "questionNav" method, but neither work. Again, I would like for the "Next" to iterate through [questionOne, questionTwo, questionThree]. I am relatively new to vue, so any help would be greatly appreciated. Thank you!