当前位置:首页 > VUE

vue实现各种问卷

2026-02-19 18:57:17VUE

Vue 实现问卷系统的方法

基础问卷表单结构

使用 Vue 的 v-model 绑定表单数据,结合 v-for 动态渲染问题列表。示例代码:

<template>
  <div v-for="(question, index) in questions" :key="index">
    <label>{{ question.text }}</label>
    <input 
      v-if="question.type === 'text'"
      v-model="answers[index]"
      type="text"
    >
    <select v-else-if="question.type === 'select'" v-model="answers[index]">
      <option v-for="opt in question.options" :value="opt">{{ opt }}</option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [
        { text: "姓名", type: "text" },
        { text: "满意度", type: "select", options: ["满意", "一般", "不满意"] }
      ],
      answers: []
    }
  }
}
</script>

动态添加问题

通过数组方法实现问题管理:

methods: {
  addQuestion() {
    this.questions.push({
      text: "",
      type: "text",
      options: []
    });
  },
  removeQuestion(index) {
    this.questions.splice(index, 1);
  }
}

表单验证

使用 Vue 的 computed 属性或第三方库如 VeeValidate:

computed: {
  isFormValid() {
    return this.answers.every(answer => answer !== "");
  }
}

保存问卷数据

通过 API 调用或本地存储:

methods: {
  submitForm() {
    axios.post('/api/survey', { answers: this.answers })
      .then(response => {
        alert('提交成功');
      });
  }
}

可视化结果展示

结合图表库如 ECharts 展示统计结果:

<template>
  <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  mounted() {
    const chart = echarts.init(this.$refs.chart);
    chart.setOption({
      xAxis: { data: ['满意', '一般', '不满意'] },
      yAxis: {},
      series: [{ data: [30, 20, 10], type: 'bar' }]
    });
  }
}
</script>

进阶功能实现

  1. 条件逻辑跳转:通过 watch 监听答案变化

    watch: {
    answers: {
     handler(newVal) {
       if (newVal[0] === '不满意') {
         this.showFollowUp = true;
       }
     },
     deep: true
    }
    }
  2. 多页问卷:使用动态组件或路由分页

    <component :is="currentPageComponent" />
  3. 拖拽排序:集成 vuedraggable

    vue实现各种问卷

    <draggable v-model="questions">
    <div v-for="(item, index) in questions" :key="index">
     {{ item.text }}
    </div>
    </draggable>

注意事项

  • 复杂问卷建议使用专业表单库如 vue-form-generator
  • 移动端适配需考虑触摸事件和响应式布局
  • 数据持久化建议结合本地存储或数据库
  • 性能优化可使用虚拟滚动处理长列表

标签: 问卷vue
分享给朋友:

相关文章

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue实现字幕

vue实现字幕

Vue 实现字幕功能 在 Vue 中实现字幕功能可以通过动态绑定数据和 CSS 动画来实现。以下是具体实现方法: 1. 基础字幕组件 创建一个字幕组件,通过 v-model 或 props 接收字…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 canvas 动态生成水印 在 Vue 组件中通过 canvas 绘制水印,并将其作为背景图片插入到目标元素中。这种方法灵活性高,支持动态调整水印内容、角度和透明度。 &…

vue实现多线程

vue实现多线程

Vue.js 本身是单线程框架,但可以通过以下方法实现类似多线程的效果或利用浏览器多线程能力: 使用 Web Worker Web Worker 是浏览器提供的多线程 API,适合处理 CPU 密集…