当前位置:首页 > VUE

vue实现投票

2026-03-07 16:49:49VUE

Vue 实现投票功能

基本结构设计

使用 Vue.js 创建一个投票组件,包含投票选项和投票按钮。通过 v-for 动态渲染选项列表,v-model 绑定用户选择的数据。

<template>
  <div class="vote-container">
    <h3>{{ title }}</h3>
    <ul>
      <li v-for="(option, index) in options" :key="index">
        <input 
          type="radio" 
          :id="'option-' + index" 
          :value="index" 
          v-model="selectedOption"
        >
        <label :for="'option-' + index">{{ option.text }}</label>
      </li>
    </ul>
    <button @click="submitVote">提交投票</button>
    <div v-if="showResult">
      <h4>投票结果</h4>
      <ul>
        <li v-for="(option, index) in options" :key="index">
          {{ option.text }}: {{ option.votes }}票
        </li>
      </ul>
    </div>
  </div>
</template>

数据与逻辑处理

script 部分定义投票数据和方法。使用 data 初始化选项列表和用户选择,通过 methods 实现投票提交逻辑。

<script>
export default {
  data() {
    return {
      title: '请选择您最喜欢的框架',
      options: [
        { text: 'Vue', votes: 0 },
        { text: 'React', votes: 0 },
        { text: 'Angular', votes: 0 }
      ],
      selectedOption: null,
      showResult: false
    }
  },
  methods: {
    submitVote() {
      if (this.selectedOption !== null) {
        this.options[this.selectedOption].votes++
        this.showResult = true
      } else {
        alert('请先选择一个选项')
      }
    }
  }
}
</script>

样式优化

添加 CSS 美化投票界面,确保选项布局清晰,按钮交互友好。

vue实现投票

<style scoped>
.vote-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}
ul {
  list-style: none;
  padding: 0;
}
li {
  margin: 10px 0;
}
button {
  background-color: #42b983;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}
button:hover {
  background-color: #369f6b;
}
</style>

进阶功能扩展

  1. 防止重复投票
    使用 localStorage 记录用户投票状态,避免重复提交。
methods: {
  submitVote() {
    if (localStorage.getItem('hasVoted')) {
      alert('您已经投过票了')
      return
    }
    if (this.selectedOption !== null) {
      this.options[this.selectedOption].votes++
      this.showResult = true
      localStorage.setItem('hasVoted', 'true')
    }
  }
}
  1. 实时数据同步
    结合 Firebase 或 WebSocket 实现多客户端实时结果更新。
import { db } from './firebase'
methods: {
  async submitVote() {
    await db.collection('votes').doc(this.selectedOption).update({
      votes: firebase.firestore.FieldValue.increment(1)
    })
  }
}
  1. 图表展示结果
    引入 ECharts 或 Chart.js 将投票数据可视化。
import * as echarts from 'echarts'
mounted() {
  if (this.showResult) {
    const chart = echarts.init(document.getElementById('chart'))
    chart.setOption({
      xAxis: { data: this.options.map(o => o.text) },
      yAxis: { type: 'value' },
      series: [{ data: this.options.map(o => o.votes), type: 'bar' }]
    })
  }
}

注意事项

  • 单选框绑定需确保 v-modelvalue 类型一致
  • 未选择选项时应给予明确提示
  • 生产环境需结合后端验证投票合法性

标签: vue
分享给朋友:

相关文章

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…

vue实现fragment

vue实现fragment

Vue 实现 Fragment 的方法 在 Vue 中,Fragment 允许组件返回多个根节点而不需要包裹一个额外的 DOM 元素。以下是几种实现方式: 使用 Vue 3 的 <templa…