当前位置:首页 > VUE

vue实现试题录入

2026-03-08 23:25:00VUE

Vue 实现试题录入功能

数据模型设计

试题数据通常包含题目、选项、答案、类型等字段。可以设计如下数据结构:

question: {
  id: '',          // 试题ID
  title: '',       // 题目内容
  type: 'single',  // 题型:single/multiple/truefalse
  options: [       // 选项数组
    { id: 1, text: '' },
    { id: 2, text: '' }
  ],
  answer: [],      // 正确答案ID数组
  difficulty: 1,   // 难度系数
  tags: []         // 标签分类
}

表单组件构建

使用Vue的v-model实现双向数据绑定:

<template>
  <div class="question-editor">
    <input v-model="question.title" placeholder="请输入题目内容">

    <select v-model="question.type">
      <option value="single">单选题</option>
      <option value="multiple">多选题</option>
      <option value="truefalse">判断题</option>
    </select>

    <div v-for="(opt, index) in question.options" :key="opt.id">
      <input v-model="opt.text" :placeholder="'选项' + (index+1)">
      <button @click="removeOption(index)">删除</button>
    </div>

    <button @click="addOption">添加选项</button>
  </div>
</template>

动态选项管理

实现选项的增删逻辑:

methods: {
  addOption() {
    const newId = this.question.options.length + 1
    this.question.options.push({
      id: newId,
      text: ''
    })
  },
  removeOption(index) {
    this.question.options.splice(index, 1)
  }
}

答案选择逻辑

根据题型显示不同的答案选择方式:

<div v-if="question.type === 'single'">
  <label v-for="opt in question.options" :key="opt.id">
    <input type="radio" 
           v-model="question.answer" 
           :value="[opt.id]">
    {{ opt.text }}
  </label>
</div>

<div v-else-if="question.type === 'multiple'">
  <label v-for="opt in question.options" :key="opt.id">
    <input type="checkbox"
           v-model="question.answer"
           :value="opt.id">
    {{ opt.text }}
  </label>
</div>

数据提交处理

添加提交方法将试题数据保存到后端:

methods: {
  async submitQuestion() {
    try {
      const res = await axios.post('/api/questions', this.question)
      console.log('试题保存成功', res.data)
      this.resetForm()
    } catch (error) {
      console.error('保存失败', error)
    }
  },
  resetForm() {
    this.question = {
      title: '',
      type: 'single',
      options: [{id:1, text:''}, {id:2, text:''}],
      answer: [],
      difficulty: 1,
      tags: []
    }
  }
}

富文本编辑器集成

如需复杂题目内容,可集成富文本编辑器:

<template>
  <quill-editor v-model="question.title" :options="editorOptions"/>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
export default {
  components: { quillEditor },
  data() {
    return {
      editorOptions: {
        modules: {
          toolbar: [
            ['bold', 'italic'],
            ['code-block']
          ]
        }
      }
    }
  }
}
</script>

表单验证

使用Vuelidate进行数据校验:

import { required, minLength } from 'vuelidate/lib/validators'

export default {
  validations: {
    question: {
      title: { required },
      options: {
        $each: {
          text: { required }
        }
      },
      answer: {
        required,
        minLength: minLength(1)
      }
    }
  }
}

组件化设计

将试题录入拆分为多个组件:

vue实现试题录入

QuestionForm.vue       - 主表单容器
QuestionTypeSelect.vue - 题型选择
OptionList.vue         - 选项管理
AnswerArea.vue         - 答案选择
TagInput.vue           - 标签输入

以上实现方案可根据实际需求进行调整,如添加图片上传、公式编辑、试题导入导出等功能。

标签: 试题vue
分享给朋友:

相关文章

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue实现频道

vue实现频道

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

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现导入

vue实现导入

Vue 实现文件导入功能 使用 <input type="file"> 元素 在 Vue 模板中添加一个文件输入元素,绑定 change 事件处理函数。 <template>…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…