vue 实现问卷
Vue 实现问卷系统
基础结构搭建
使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 vue-router、element-ui 等)。创建以下核心组件:
Questionnaire.vue(问卷容器)Question.vue(单个问题组件)Result.vue(结果页)
// 路由配置示例
const routes = [
{ path: '/', component: Questionnaire },
{ path: '/result', component: Result }
]
动态问题渲染
通过 v-for 循环渲染问题列表,根据问题类型(单选、多选、文本)动态切换组件:
<template>
<div v-for="(question, index) in questions" :key="index">
<SingleChoice v-if="question.type === 'radio'" :question="question"/>
<TextInput v-else-if="question.type === 'text'" :question="question"/>
</div>
</template>
状态管理
使用 Vuex 或 Pinia 管理问卷状态,存储用户答案和问题数据:
// Pinia 示例
export const useStore = defineStore('questionnaire', {
state: () => ({
answers: {},
questions: [
{ id: 1, text: "您的年龄?", type: "radio", options: ["18-25", "26-35"] }
]
}),
actions: {
setAnswer(questionId, answer) {
this.answers[questionId] = answer
}
}
})
表单验证
利用 v-model 绑定数据和 Vuelidate 进行验证:
import { required } from '@vuelidate/validators'
export default {
validations() {
return {
answer: { required }
}
}
}
结果提交
通过 Axios 将结果提交至后端 API,或生成本地报告:
methods: {
submit() {
axios.post('/api/submit', this.$store.answers)
.then(() => this.$router.push('/result'))
}
}
响应式设计
使用 CSS Flex/Grid 或 UI 库(如 Element-UI)确保移动端适配:

@media (max-width: 768px) {
.question { padding: 10px; }
}
进阶功能
- 问题逻辑跳转:根据答案动态切换下一题
- 本地存储:通过
localStorage保存未完成的问卷 - 进度条:计算已回答问题比例
- 导出结果:生成 PDF 或 Excel 报告
完整实现需结合具体业务需求调整组件结构和状态管理方案。






