当前位置:首页 > 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中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现按卡片轮播

vue实现按卡片轮播

实现卡片轮播的基本思路 在Vue中实现卡片轮播可以通过结合v-for指令和动态样式绑定完成。核心是维护一个当前显示卡片的索引,通过CSS过渡效果实现平滑切换。 基础实现步骤 模板部分 使用v-fo…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…