当前位置:首页 > VUE

vue答题功能实现

2026-02-18 08:45:39VUE

Vue 答题功能实现方案

数据结构和状态管理

使用 Vue 的 dataref(Composition API)管理题目和用户答案:

data() {
  return {
    questions: [
      {
        id: 1,
        text: "问题1",
        options: ["A", "B", "C"],
        answer: "A"
      }
    ],
    currentIndex: 0,
    userAnswers: {}
  }
}

题目渲染组件

通过 v-for 动态渲染题目和选项:

vue答题功能实现

<template>
  <div v-for="(q, index) in questions" :key="q.id">
    <h3>{{ q.text }}</h3>
    <div v-for="option in q.options" :key="option">
      <input 
        type="radio" 
        :name="'q'+index" 
        :value="option"
        v-model="userAnswers[index]">
      {{ option }}
    </div>
  </div>
</template>

答题逻辑处理

计算得分和验证答案:

methods: {
  checkAnswers() {
    let score = 0
    this.questions.forEach((q, index) => {
      if (this.userAnswers[index] === q.answer) {
        score++
      }
    })
    return score
  }
}

进度控制

添加题目导航按钮:

vue答题功能实现

<button @click="currentIndex--" :disabled="currentIndex === 0">上一题</button>
<button @click="currentIndex++" :disabled="currentIndex >= questions.length-1">下一题</button>

结果展示

使用计算属性显示结果:

computed: {
  result() {
    const correct = this.checkAnswers()
    return `正确 ${correct}/${this.questions.length}`
  }
}

进阶功能建议

  • 使用 Vuex/Pinia 管理全局状态
  • 添加计时器功能
  • 实现题目分类和难度分级
  • 加入动画过渡效果
  • 支持多种题型(多选题、判断题等)

样式优化

建议使用 CSS 模块化或 SCSS:

.question {
  margin-bottom: 20px;
}
.options {
  display: flex;
  flex-direction: column;
}

以上方案可根据实际需求进行调整,核心在于响应式数据绑定和组件化设计。

标签: 功能vue
分享给朋友:

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…