当前位置:首页 > VUE

vue实现投票

2026-03-07 16:49:49VUE

Vue 实现投票功能

基本结构设计

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

vue实现投票

<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 实现投票提交逻辑。

vue实现投票

<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 美化投票界面,确保选项布局清晰,按钮交互友好。

<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实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue实现座位

vue实现座位

Vue实现座位布局 使用Vue实现座位布局可以通过组件化和响应式数据管理来简化开发流程。以下是一个完整的实现方案: 数据准备 定义座位数据结构,通常使用二维数组表示行列关系: data() {…