当前位置:首页 > VUE

vue怎么实现预约就诊

2026-02-24 22:07:45VUE

Vue 实现预约就诊功能

预约就诊功能通常需要结合前端 Vue 和后端 API 实现,以下是关键步骤和代码示例:

创建预约表单组件

<template>
  <div class="appointment-form">
    <h3>预约就诊</h3>
    <form @submit.prevent="submitAppointment">
      <div class="form-group">
        <label>选择科室</label>
        <select v-model="form.department" required>
          <option v-for="dept in departments" :key="dept.id" :value="dept.id">
            {{ dept.name }}
          </option>
        </select>
      </div>

      <div class="form-group">
        <label>选择医生</label>
        <select v-model="form.doctor" required>
          <option v-for="doc in doctors" :key="doc.id" :value="doc.id">
            {{ doc.name }} - {{ doc.specialty }}
          </option>
        </select>
      </div>

      <div class="form-group">
        <label>预约日期</label>
        <input type="date" v-model="form.date" required>
      </div>

      <div class="form-group">
        <label>预约时间</label>
        <select v-model="form.time" required>
          <option v-for="slot in timeSlots" :key="slot" :value="slot">
            {{ slot }}
          </option>
        </select>
      </div>

      <button type="submit">提交预约</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      departments: [],
      doctors: [],
      timeSlots: ['09:00', '10:00', '11:00', '14:00', '15:00', '16:00'],
      form: {
        department: '',
        doctor: '',
        date: '',
        time: ''
      }
    }
  },
  methods: {
    async fetchDepartments() {
      const response = await axios.get('/api/departments')
      this.departments = response.data
    },
    async fetchDoctors(departmentId) {
      const response = await axios.get(`/api/doctors?department=${departmentId}`)
      this.doctors = response.data
    },
    async submitAppointment() {
      try {
        await axios.post('/api/appointments', this.form)
        alert('预约成功!')
        this.$router.push('/appointments')
      } catch (error) {
        console.error('预约失败:', error)
      }
    }
  },
  watch: {
    'form.department'(newVal) {
      if (newVal) this.fetchDoctors(newVal)
    }
  },
  created() {
    this.fetchDepartments()
  }
}
</script>

实现预约管理页面

<template>
  <div class="appointment-list">
    <h3>我的预约</h3>
    <table>
      <thead>
        <tr>
          <th>科室</th>
          <th>医生</th>
          <th>日期</th>
          <th>时间</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="appointment in appointments" :key="appointment.id">
          <td>{{ appointment.departmentName }}</td>
          <td>{{ appointment.doctorName }}</td>
          <td>{{ formatDate(appointment.date) }}</td>
          <td>{{ appointment.time }}</td>
          <td>{{ appointment.status }}</td>
          <td>
            <button @click="cancelAppointment(appointment.id)" 
                    v-if="appointment.status === '待就诊'">
              取消
            </button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      appointments: []
    }
  },
  methods: {
    async fetchAppointments() {
      const response = await axios.get('/api/appointments')
      this.appointments = response.data
    },
    async cancelAppointment(id) {
      if (confirm('确定要取消预约吗?')) {
        await axios.delete(`/api/appointments/${id}`)
        this.fetchAppointments()
      }
    },
    formatDate(dateString) {
      return new Date(dateString).toLocaleDateString()
    }
  },
  created() {
    this.fetchAppointments()
  }
}
</script>

添加路由配置

// router.js
import Vue from 'vue'
import Router from 'vue-router'
import AppointmentForm from './components/AppointmentForm.vue'
import AppointmentList from './components/AppointmentList.vue'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/appointments',
      component: AppointmentList
    },
    {
      path: '/book-appointment',
      component: AppointmentForm
    }
  ]
})

实现状态管理(可选)

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

vue怎么实现预约就诊

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    appointments: [],
    departments: [],
    doctors: []
  },
  mutations: {
    SET_APPOINTMENTS(state, appointments) {
      state.appointments = appointments
    },
    SET_DEPARTMENTS(state, departments) {
      state.departments = departments
    },
    SET_DOCTORS(state, doctors) {
      state.doctors = doctors
    }
  },
  actions: {
    async fetchAppointments({ commit }) {
      const response = await axios.get('/api/appointments')
      commit('SET_APPOINTMENTS', response.data)
    },
    async fetchDepartments({ commit }) {
      const response = await axios.get('/api/departments')
      commit('SET_DEPARTMENTS', response.data)
    },
    async fetchDoctors({ commit }, departmentId) {
      const response = await axios.get(`/api/doctors?department=${departmentId}`)
      commit('SET_DOCTORS', response.data)
    }
  }
})

后端 API 建议

  1. 科室管理接口

    vue怎么实现预约就诊

    • GET /api/departments - 获取所有科室
  2. 医生管理接口

    • GET /api/doctors - 根据科室筛选医生
  3. 预约管理接口

    • POST /api/appointments - 创建预约
    • GET /api/appointments - 获取用户预约列表
    • DELETE /api/appointments/:id - 取消预约

功能增强建议

  1. 添加日期验证,禁止选择过去的日期
  2. 实现时间段冲突检测
  3. 添加医生排班信息展示
  4. 集成短信/邮件通知功能
  5. 添加预约提醒功能
  6. 实现分时段预约限制(如每半小时限制人数)

以上实现可根据实际需求进行调整和扩展,核心是处理好表单数据收集、API 交互和状态管理三个部分。

标签: vue
分享给朋友:

相关文章

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue 实现弹窗

vue 实现弹窗

Vue 实现弹窗的方法 在 Vue 中实现弹窗功能可以通过多种方式完成,以下是几种常见的方法: 使用组件和 v-if/v-show 控制显示 创建一个独立的弹窗组件,通过 v-if 或 v-show…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 push…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…