当前位置:首页 > VUE

vue预约实现

2026-03-28 02:19:04VUE

Vue 预约功能实现

在 Vue 中实现预约功能通常涉及表单处理、时间选择、数据提交和状态管理。以下是实现预约功能的关键步骤:

表单设计与数据绑定

使用 Vue 的 v-model 实现表单数据双向绑定,创建包含姓名、联系方式、预约日期和时间的表单:

<template>
  <form @submit.prevent="submitForm">
    <input v-model="form.name" placeholder="姓名" required>
    <input v-model="form.phone" placeholder="电话" required>
    <input type="date" v-model="form.date" required>
    <select v-model="form.time" required>
      <option v-for="time in availableTimes" :value="time">{{ time }}</option>
    </select>
    <button type="submit">提交预约</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: '',
        phone: '',
        date: '',
        time: ''
      },
      availableTimes: ['09:00', '10:00', '11:00', '14:00', '15:00']
    }
  }
}
</script>

日期时间处理

引入第三方日期时间选择器如 vue-datepickerelement-ui 的日期组件增强用户体验:

vue预约实现

import DatePicker from 'vue-datepicker'

export default {
  components: { DatePicker },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      }
    }
  }
}

表单验证

使用 Vuelidate 或自定义验证方法确保输入有效性:

validations: {
  form: {
    name: { required },
    phone: { required, numeric, minLength: minLength(10) },
    date: { required },
    time: { required }
  }
},
methods: {
  submitForm() {
    if(!this.$v.$invalid) {
      this.saveReservation()
    }
  }
}

数据提交与存储

通过 axios 将预约数据发送到后端 API 或存储在本地:

vue预约实现

methods: {
  async saveReservation() {
    try {
      const response = await axios.post('/api/reservations', this.form)
      this.$router.push('/success')
    } catch (error) {
      console.error('预约失败:', error)
    }
  }
}

预约状态管理

对于复杂应用,使用 Vuex 管理预约状态:

const store = new Vuex.Store({
  state: {
    reservations: []
  },
  mutations: {
    ADD_RESERVATION(state, payload) {
      state.reservations.push(payload)
    }
  },
  actions: {
    addReservation({ commit }, reservation) {
      commit('ADD_RESERVATION', reservation)
    }
  }
})

用户反馈

添加 Toast 或弹窗通知用户操作结果:

this.$toast.success('预约成功!', { position: 'top-right' })

预约列表展示

创建组件显示已有预约:

<template>
  <div v-for="reservation in reservations" :key="reservation.id">
    <p>{{ reservation.name }} - {{ reservation.date }} {{ reservation.time }}</p>
  </div>
</template>

实现完整的预约系统需要考虑实际业务需求,如预约冲突检测、取消功能、提醒通知等。以上代码提供了核心功能的实现框架,可根据具体项目需求进行调整扩展。

标签: vue
分享给朋友:

相关文章

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…

vue实现dag

vue实现dag

Vue实现DAG(有向无环图) 在Vue中实现DAG(Directed Acyclic Graph,有向无环图)通常涉及数据结构的建模、可视化渲染以及交互逻辑处理。以下是关键实现步骤和示例代码: 数…