当前位置:首页 > 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>

动态选项管理

实现选项的增删逻辑:

vue实现试题录入

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>

数据提交处理

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

vue实现试题录入

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

组件化设计

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

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

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

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

相关文章

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue嵌套grafana实现

vue嵌套grafana实现

Vue 中嵌套 Grafana 的实现方法 使用 iframe 嵌入 Grafana 面板 在 Vue 项目中可以通过 iframe 直接嵌入 Grafana 的面板或仪表板。确保 Grafana 已…