当前位置:首页 > 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中配置请假相关状态和操作:

vue实现销假功能

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

路由配置

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

vue实现销假功能

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实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…