当前位置:首页 > VUE

vue实现销假功能

2026-03-30 05:10:42VUE

实现销假功能的Vue方案

销假功能通常涉及表单提交、数据验证和与后端API的交互。以下是基于Vue 3的完整实现方案。

表单设计与数据绑定

创建销假申请表单组件,包含请假类型、时间范围、原因等字段。使用v-model进行双向数据绑定:

<template>
  <form @submit.prevent="submitLeaveRequest">
    <select v-model="form.leaveType">
      <option value="annual">年假</option>
      <option value="sick">病假</option>
    </select>

    <input type="date" v-model="form.startDate">
    <input type="date" v-model="form.endDate">

    <textarea v-model="form.reason"></textarea>

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

<script setup>
import { reactive } from 'vue';

const form = reactive({
  leaveType: 'annual',
  startDate: '',
  endDate: '',
  reason: ''
});
</script>

表单验证处理

使用Vuelidate或自定义验证方法确保数据有效性:

const validateForm = () => {
  const errors = [];
  if (!form.startDate) errors.push('请选择开始日期');
  if (form.endDate < form.startDate) errors.push('结束日期不能早于开始日期');
  return errors;
};

API请求封装

创建独立服务文件处理HTTP请求:

// leaveService.js
import axios from 'axios';

export default {
  async submitLeaveApplication(data) {
    try {
      const response = await axios.post('/api/leaves', data);
      return response.data;
    } catch (error) {
      throw new Error('提交失败: ' + error.message);
    }
  }
}

状态管理集成

对于复杂应用,建议使用Pinia管理销假状态:

// stores/leaveStore.js
import { defineStore } from 'pinia';

export const useLeaveStore = defineStore('leave', {
  state: () => ({
    applications: []
  }),
  actions: {
    async submitApplication(formData) {
      const response = await leaveService.submitLeaveApplication(formData);
      this.applications.push(response);
    }
  }
});

用户反馈与交互

提交后提供可视化反馈:

<script setup>
import { useLeaveStore } from '@/stores/leaveStore';

const store = useLeaveStore();
const submitLeaveRequest = async () => {
  const errors = validateForm();
  if (errors.length > 0) {
    alert(errors.join('\n'));
    return;
  }

  try {
    await store.submitApplication(form);
    alert('销假申请提交成功');
    form.value = {}; // 重置表单
  } catch (error) {
    alert(error.message);
  }
};
</script>

响应式布局优化

使用UI框架如Element Plus增强用户体验:

<template>
  <el-form :model="form" label-width="120px">
    <el-form-item label="请假类型">
      <el-select v-model="form.leaveType">
        <el-option label="年假" value="annual"></el-option>
        <el-option label="病假" value="sick"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="开始日期">
      <el-date-picker v-model="form.startDate" type="date"></el-date-picker>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitLeaveRequest">提交</el-button>
    </el-form-item>
  </el-form>
</template>

历史记录展示

添加申请历史查看功能:

vue实现销假功能

<template>
  <div v-for="app in store.applications" :key="app.id">
    {{ app.leaveType }} - {{ app.status }}
  </div>
</template>

实现完整的销假功能需要前后端配合,前端主要负责数据收集、验证和展示,后端处理业务逻辑和持久化存储。根据实际需求可增加审批流程、附件上传等扩展功能。

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

相关文章

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…

vue 实现页面注册

vue 实现页面注册

Vue 实现页面注册功能 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,通常命名为 Register.vue。该组件包含用户名、邮箱、密码等输入字段,以及提交按钮。 <templa…