当前位置:首页 > 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 管理预约状态:

// 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. 科室管理接口

    • 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 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <tem…

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用i…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…