当前位置:首页 > VUE

vue实现购票

2026-03-07 06:10:14VUE

Vue 实现购票功能

项目结构设计

  • 使用 Vue CLI 创建项目
  • 采用 Vuex 管理状态(如座位选择、票价计算)
  • 使用 Vue Router 实现页面导航

核心功能实现

座位选择组件

vue实现购票

<template>
  <div class="seat-map">
    <div v-for="(row, rowIndex) in seats" :key="rowIndex" class="seat-row">
      <div 
        v-for="(seat, colIndex) in row" 
        :key="colIndex"
        :class="['seat', seat.status]"
        @click="selectSeat(rowIndex, colIndex)"
      >
        {{ seat.number }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      seats: [
        [{number: 'A1', status: 'available'}, ...],
        // 更多座位数据
      ]
    }
  },
  methods: {
    selectSeat(row, col) {
      if(this.seats[row][col].status === 'available') {
        this.$store.commit('addSelectedSeat', this.seats[row][col])
        this.seats[row][col].status = 'selected'
      }
    }
  }
}
</script>

购物车组件

vue实现购票

<template>
  <div class="cart">
    <h3>已选座位</h3>
    <ul>
      <li v-for="(seat, index) in selectedSeats" :key="index">
        {{ seat.number }} - ¥{{ seat.price }}
        <button @click="removeSeat(index)">移除</button>
      </li>
    </ul>
    <div class="total">总计: ¥{{ totalPrice }}</div>
    <button @click="checkout">结算</button>
  </div>
</template>

<script>
export default {
  computed: {
    selectedSeats() {
      return this.$store.state.selectedSeats
    },
    totalPrice() {
      return this.selectedSeats.reduce((sum, seat) => sum + seat.price, 0)
    }
  },
  methods: {
    removeSeat(index) {
      this.$store.commit('removeSelectedSeat', index)
    },
    checkout() {
      this.$router.push('/payment')
    }
  }
}
</script>

状态管理(Vuex)

// store.js
export default new Vuex.Store({
  state: {
    selectedSeats: []
  },
  mutations: {
    addSelectedSeat(state, seat) {
      state.selectedSeats.push(seat)
    },
    removeSelectedSeat(state, index) {
      state.selectedSeats.splice(index, 1)
    },
    clearCart(state) {
      state.selectedSeats = []
    }
  }
})

支付流程实现

支付页面组件

<template>
  <div class="payment">
    <h2>支付信息</h2>
    <form @submit.prevent="submitPayment">
      <input type="text" v-model="cardNumber" placeholder="信用卡号">
      <input type="text" v-model="expiry" placeholder="有效期">
      <input type="text" v-model="cvv" placeholder="CVV">
      <button type="submit">确认支付</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cardNumber: '',
      expiry: '',
      cvv: ''
    }
  },
  methods: {
    submitPayment() {
      // 调用API处理支付
      this.$store.dispatch('processPayment', {
        seats: this.$store.state.selectedSeats,
        paymentInfo: {
          cardNumber: this.cardNumber,
          expiry: this.expiry,
          cvv: this.cvv
        }
      }).then(() => {
        this.$router.push('/confirmation')
      })
    }
  }
}
</script>

响应式设计要点

  • 使用 Flexbox/Grid 布局确保座位图在不同设备上正常显示
  • 添加媒体查询调整小屏幕下的布局
  • 为触摸设备优化点击区域大小

性能优化建议

  • 虚拟滚动处理大量座位渲染
  • 懒加载非首屏组件
  • 使用 keep-alive 缓存常用组件

安全注意事项

  • 支付信息通过 HTTPS 传输
  • 敏感数据不存储在客户端
  • 实现 CSRF 防护

这个实现包含了购票系统的主要功能模块,可根据实际需求扩展更多特性如优惠券、座位推荐等功能。

标签: vue
分享给朋友:

相关文章

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…

vue实现烟花

vue实现烟花

Vue 实现烟花效果 在 Vue 中实现烟花效果可以通过结合 CSS 动画和 JavaScript 动态生成烟花元素。以下是具体实现方法: 动态生成烟花元素 通过 Vue 的 v-for 动态生成烟…

vue实现字幕

vue实现字幕

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

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…

vue实现单词拼写

vue实现单词拼写

Vue 实现单词拼写功能 在 Vue 中实现单词拼写功能,可以通过数据绑定、事件处理和动态样式来实现。以下是一个完整的实现方案。 数据准备 定义一个包含单词和提示信息的数组,用于拼写练习: dat…