当前位置:首页 > VUE

vue实现销假功能

2026-01-17 11:58:53VUE

实现销假功能的Vue方案

在Vue中实现销假功能通常需要结合前端界面、状态管理和后端API交互。以下是完整的实现方案:

基础组件结构

创建请假表单组件LeaveForm.vue

<template>
  <form @submit.prevent="submitLeave">
    <div>
      <label>请假类型</label>
      <select v-model="form.leaveType">
        <option value="annual">年假</option>
        <option value="sick">病假</option>
      </select>
    </div>

    <div>
      <label>开始时间</label>
      <input type="datetime-local" v-model="form.startTime">
    </div>

    <div>
      <label>结束时间</label>
      <input type="datetime-local" v-model="form.endTime">
    </div>

    <button type="submit">提交申请</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        leaveType: 'annual',
        startTime: '',
        endTime: ''
      }
    }
  },
  methods: {
    async submitLeave() {
      try {
        await this.$store.dispatch('applyLeave', this.form)
        alert('申请成功')
      } catch (error) {
        console.error(error)
      }
    }
  }
}
</script>

状态管理(Vuex)

在store中配置请假相关状态和操作:

// store/modules/leave.js
export default {
  state: {
    leaves: [],
    isLoading: false
  },
  mutations: {
    SET_LEAVES(state, leaves) {
      state.leaves = leaves
    },
    SET_LOADING(state, status) {
      state.isLoading = status
    }
  },
  actions: {
    async applyLeave({ commit }, formData) {
      commit('SET_LOADING', true)
      const response = await api.post('/leaves', formData)
      commit('SET_LOADING', false)
      return response
    },

    async cancelLeave({ commit }, leaveId) {
      commit('SET_LOADING', true)
      await api.delete(`/leaves/${leaveId}`)
      commit('SET_LOADING', false)
    }
  }
}

销假功能实现

创建销假组件CancelLeave.vue

<template>
  <div v-if="leaves.length">
    <h3>我的请假记录</h3>
    <ul>
      <li v-for="leave in leaves" :key="leave.id">
        {{ leave.type }} - {{ leave.start }}至{{ leave.end }}
        <button @click="cancelLeave(leave.id)">撤销申请</button>
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  computed: {
    ...mapState('leave', ['leaves'])
  },
  methods: {
    ...mapActions('leave', ['cancelLeave']),
    async loadLeaves() {
      await this.$store.dispatch('leave/fetchLeaves')
    }
  },
  created() {
    this.loadLeaves()
  }
}
</script>

路由配置

在路由文件中配置相关页面:

const routes = [
  {
    path: '/leave/apply',
    component: () => import('./views/LeaveForm.vue')
  },
  {
    path: '/leave/cancel',
    component: () => import('./views/CancelLeave.vue')
  }
]

API服务层

创建API服务文件api/leave.js

import axios from 'axios'

export default {
  applyLeave(data) {
    return axios.post('/api/leaves', data)
  },

  cancelLeave(id) {
    return axios.delete(`/api/leaves/${id}`)
  },

  getLeaves() {
    return axios.get('/api/leaves')
  }
}

表单验证

为请假表单添加验证逻辑:

// 在LeaveForm组件中添加
methods: {
  validateForm() {
    if (!this.form.startTime || !this.form.endTime) {
      throw new Error('请选择完整时间范围')
    }
    if (new Date(this.form.startTime) > new Date(this.form.endTime)) {
      throw new Error('开始时间不能晚于结束时间')
    }
  },

  async submitLeave() {
    try {
      this.validateForm()
      await this.$store.dispatch('applyLeave', this.form)
      this.$router.push('/leave/cancel')
    } catch (error) {
      alert(error.message)
    }
  }
}

后端接口规范

建议后端接口遵循RESTful规范:

  • POST /api/leaves - 创建请假
  • DELETE /api/leaves/:id - 撤销请假
  • GET /api/leaves - 获取请假列表

注意事项

  1. 时间处理建议使用moment.js或day.js库
  2. 生产环境需要添加更完善的错误处理
  3. 敏感操作建议添加二次确认对话框
  4. 列表数据需要分页处理
  5. 用户权限验证必不可少

以上方案可根据实际项目需求进行调整,核心在于通过Vue组件组织UI,Vuex管理状态,axios处理API交互,实现完整的销假功能流程。

vue实现销假功能

标签: 功能vue
分享给朋友:

相关文章

vue实现多级组件

vue实现多级组件

实现多级组件的基本结构 在Vue中实现多级组件通常涉及父子组件嵌套,通过props向下传递数据,通过$emit向上传递事件。多级组件的核心是递归或动态组件渲染。 <!-- 父组件 Parent…

前端实现拖拽功能vue

前端实现拖拽功能vue

实现拖拽功能的基本方法 在Vue中实现拖拽功能可以通过HTML5的拖放API或第三方库如vuedraggable来完成。HTML5的拖放API提供了原生支持,而vuedraggable则简化了列表拖拽…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-s…