当前位置:首页 > VUE

vue实现购票

2026-03-07 06:10:14VUE

Vue 实现购票功能

项目结构设计

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

核心功能实现

座位选择组件

<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>

购物车组件

<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 = []
    }
  }
})

支付流程实现

支付页面组件

vue实现购票

<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懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…