当前位置:首页 > 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   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…