javascript - Vue.js - 单击按钮时迭代数据对象

标签 javascript vue.js methods iterator vue-component

我正在尝试使用 vue.js 创建一个测验,但似乎无法弄清楚如何使“下一步”按钮迭代我的数据。希望屏幕显示包含问题和答案的对象(即问题一、问题二),然后在单击“下一步”时显示下一个对象。正如您将看到的,我在现有代码中进行了多次尝试,但没有成功。

测验组件模板:

<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>

测验脚本:

<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>

如您所见,我尝试了“qNext”方法和“questionNav”方法,但都不起作用。再次,我希望“下一个”能够迭代 [questionOne、questionTwo、questionThree]。我对 vue 比较陌生,所以任何帮助将不胜感激。谢谢!

最佳答案

当前实现的问题是您尝试在 questionOnequestionTwo 的上下文中填充 questionParts QuestionThree 不可访问,这意味着您的问题数组将填充未定义的值。

总的来说,只要您确保 questionParts 确实包含问题对象,您所做的工作就应该有效。如果您想将该逻辑保留在 data 方法中,可以按以下方法操作:

data: () => {
  const questionOne = {
    q: 'How many minutes do you want to spend?' ,
    ans: ['Short (15-20)', 'Medium (20-40)', 'Long (40-60)']
  };
  const questionTwo = {
    q: 'What muscle group do you want to focus on?' ,
    ans: ['Upper Body', 'Lower Body', 'Core', 'Full Body']
  };
  const questionThree = {
    q: 'What level intensity do you want?' ,
    ans: ['Leisure Walking', 'Speed Walking', 'Jogging', 'Sprinting']
  };
  const questionParts = [questionOne, questionTwo, questionThree]
  return {
    curr: 0,
    currentQuestion: {
      q: 'kasjdn' ,
      ans: ['1', '2', '3']
    },
    questionOne,
    questionTwo,
    questionThree,
    questionParts,
  }
},

通过在实际返回 data() 的值之前声明一些变量,您可以正确填充 questionParts 数组。这应该足以让您的测验发挥作用。

您可能还需要考虑一些其他改进,例如:

  • 您可以直接实例化问题数组,而不是声明 questionOnequestionTwoquestionThree 对象。鉴于您提供的代码示例,将每个问题作为不同的对象似乎并不是特别有用。
  • currentQuestion 可以是一个计算属性,返回索引 curr 处的问题。然后,您将只能在点击处理程序中递增或递减 curr,并且计算属性将负责返回正确的问题,而无需显式分配 currentQuestion

关于javascript - Vue.js - 单击按钮时迭代数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61553075/

相关文章:

javascript - React 生命周期循环

javascript - 如何将日期字符串转换为带时区的日期?

vue.js - Vuetify 从日期选择器组件中删除正文标题月份和年份元素

python - numpy arange函数构建数组大小错误

php - 静态方法与非静态方法

java - 数组列表中的子类元素不会使用 Java 中的重写方法

javascript - 登录单页应用程序的最佳方式是什么

javascript - 从 python selenium 调用中获取一个 javascript 变量

javascript - 将值从组件 data() 传递到其他文件

javascript - Vue 对数组进行排序,后跟索引 (0, 1, 2, 3 ...)