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

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

注意事项

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

vue实现各种问卷

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

相关文章

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 ge…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…