iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue-antd form组件封装全过程
  • 615
分享到

vue-antd form组件封装全过程

vue-antd form组件封装form组件封装vue-antd form组件 2023-05-17 08:05:59 615人浏览 安东尼
摘要

目录表单项–公用组件(Vue-antd)1、废话不多说,先看具体效果2、组件代码如下3、在相应地方进行引入总结表单项–公用组件(vue-antd) 1、废话不

表单项–公用组件(vue-antd)

1、废话不多说,先看具体效果

效果图

具体组件图

表单校验图:

校验图

校验均写在组件之中,跟当前页面文件无任何关系,当前页面只接受组件传递过来的参数…

2、组件代码如下

在vue项目中,此处采.js文件形式写组件,并没用.vue文件去写…将表单项中的input和change事件暴露出来。。。

export default {
    props: ['value'],
    template: `
    <div>
        <p v-if='typeId == 1' style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计到达蒸罐地时间:</span>
            <a-date-picker 
                show-time 
                :value='SteamtankTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleSteamtankTimeInput"
            />
        </p>
        <p v-if='typeId == 1' style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
            <span>预计蒸罐所需时间:</span>
            <a-col :span="8">
                <a-input
                    addon-after="小时"
                    :value='tankTime'
                    :disabled='needInput'
                    @change='handleTankTime'
                />
            </a-col>
        </p>
        <p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计发车时间:</span>
            <a-date-picker 
                show-time 
                :value='DepartureTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleDepartureTimeInput"
            />
        </p>
        <p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
            <span>预计到装货地所需时间:</span>
            <a-col :span="8">
                <a-input 
                    addon-after="小时"
                    :disabled='needInput'
                    :value='needLoadAdressTime'
                    @change='handleNeedLoadAdressTime'
                />
            </a-col>
        </p>
        <p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计到达装货地时间:</span>
            <a-date-picker 
                show-time 
                :value='LoadAddressTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleLoadAddressTimeInput"
            />
        </p>
        <p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
            <span>预计装货所需时间:</span>
            <a-col :span="8">
                <a-input 
                    addon-after="小时"
                    :value='loadTime'
                    :disabled='needInput'
                    @change='handleLoadFinishTime'
                />
            </a-col>
        </p>
        <p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计装货完成时间:</span>
            <a-date-picker 
                show-time 
                :value='LoadFinishTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleLoadFinishTimeInput"
            />
        </p>
        <p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
            <span>预计路途所需时间:</span>
            <a-col :span="8">
                <a-input 
                    addon-after="小时"
                    :value='roadTime'
                    :disabled='needInput'
                    @change='handleroadFinishTime'
                />
            </a-col>
        </p>
        <p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计到达卸货地时间:</span>
            <a-date-picker 
                show-time 
                :value='UnloadAddressTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleUnloadAddressTimeInput"
            />
        </p>
        <p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
            <span>预计卸货所需时间:</span>
            <a-col :span="8">
                <a-input 
                    addon-after="小时"
                    :value='unLoadTime'
                    :disabled='needInput'
                    @change='handleUnLoadFinishTime'
                />
            </a-col>
        </p>
        <p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
            <span>预计卸货完成时间:</span>
            <a-date-picker 
                show-time 
                :value='UnloadFinishTimeVal'
                :allowClear="false"
                placeholder="选择时间" 
                @change="handleUnloadFinishTimeInput"
            />
        </p>
    </div>
     `,
    watch: {
        value(val = {}) {
            this.typeId = val.typeId || null
            this.tankTime = val.tankTime || null
            this.loadTime = val.loadTime || null
            this.unLoadTime = val.unLoadTime || null
            this.needLoadAdressTime = val.needLoadAdressTime || null
            this.roadTime = val.roadTime || null
            this.SteamtankTimeVal = val.steamtankTime || null
            this.DepartureTimeVal = val.departureTime || null
            this.LoadAddressTimeVal = val.loadAddressTime || null
            this.LoadFinishTimeVal = val.loadFinishTime || null
            this.UnloadAddressTimeVal = val.unLoadAddressTime || null
            this.UnloadFinishTimeVal = val.unLoadFinishTime || null
        }
    },
    data() {
        let {
            steamtankTime,
            departureTime,
            unLoadFinishTime,
            unLoadAddressTime,
            loadAddressTime,
            loadFinishTime,
            ydccId,
            typeId,
            tankTime,
            loadTime,
            unLoadTime,
            needLoadAdressTime,
            roadTime
        } = this.value
        return {
            moment,
            ydccId: ydccId || null,
            typeId: typeId || null,
            SteamtankTimeVal: steamtankTime || null,
            DepartureTimeVal: departureTime || null,
            LoadAddressTimeVal: loadAddressTime || null,
            LoadFinishTimeVal: loadFinishTime || null,
            UnloadAddressTimeVal: unLoadAddressTime || null,
            UnloadFinishTimeVal: unLoadFinishTime || null,
            tankTime: tankTime || null,
            loadTime: loadTime || null,
            unLoadTime: unLoadTime || null,
            needLoadAdressTime: needLoadAdressTime || null,
            roadTime: roadTime || null,
            minutes30: 1800000,
            hour2: 7200000,
            needInput: true,
        }
    },
    methods: {
        async getLoadTime(steamTankTime) {
            let minutes30 = this.minutes30
            let hour2 = this.hour2
            let params = {
                ydccId: this.ydccId,
                steamTankAddressId: ''
            }
            let result = await predictYdccTime(params)
            if (result.code == 200) {
                let streamAddressToSendAddressTime = Number(result.data.streamAddressToSendAddressTime) * 1.8 //蒸罐地到发货地时间(ms)
                let ydccSendToRecTime = Number(result.data.ydccSendToRecTime) * 1.8 //发货地到收货地时间(ms)
                this.needLoadAdressTime = ((streamAddressToSendAddressTime) / 3600000).toFixed(2)
                this.roadTime = ((ydccSendToRecTime - hour2) / 3600000).toFixed(2)

                this.DepartureTimeVal = moment(Number(steamTankTime) + minutes30)
                this.LoadAddressTimeVal = moment(Number(moment(this.DepartureTimeVal).fORMat('x')) + streamAddressToSendAddressTime)
                this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + hour2)
                this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + ydccSendToRecTime)
                this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + hour2)
                let dataTime = {
                    steamtankTime: moment(steamTankTime),
                    departureTime: this.DepartureTimeVal,
                    loadAddressTime: this.LoadAddressTimeVal,
                    loadFinishTime: this.LoadFinishTimeVal,
                    unLoadAddressTime: this.UnloadAddressTimeVal,
                    unLoadFinishTime: this.UnloadFinishTimeVal,
                    typeId: 1,
                    needLoadAdressTime: this.needLoadAdressTime,
                    roadTime: this.roadTime
                }
                this.triggerChange(dataTime)
                this.needInput = false
            } else {
                //.....
            }
        },
        async getDepartureTime(departureTime) {
            let hour2 = this.hour2
            let params = {
                ydccId: this.ydccId,
                steamTankAddressId: ''
            }
            let result = await predictYdccTime(params)
            if (result.code == 200) {
                let streamAddressToSendAddressTime = Number(result.data.streamAddressToSendAddressTime) * 1.8 //发车到发货地时间(ms)
                let ydccSendToRecTime = Number(result.data.ydccSendToRecTime) * 1.8 //发货地到收货地时间(ms)
                this.needLoadAdressTime = (0 * 3600000).toFixed(2)
                this.roadTime = ((ydccSendToRecTime - hour2) / 3600000).toFixed(2)
                this.LoadAddressTimeVal = moment(Number(departureTime) + streamAddressToSendAddressTime)
                this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + hour2)
                this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + ydccSendToRecTime)
                this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + hour2)
                let dataTime = {
                    steamtankTime: moment(departureTime),
                    departureTime: this.DepartureTimeVal,
                    loadAddressTime: this.LoadAddressTimeVal,
                    loadFinishTime: this.LoadFinishTimeVal,
                    unLoadAddressTime: this.UnloadAddressTimeVal,
                    unLoadFinishTime: this.UnloadFinishTimeVal,
                    typeId: 0,
                    needLoadAdressTime: this.needLoadAdressTime,
                    roadTime: this.roadTime
                }
                this.triggerChange(dataTime)
                this.needInput = false
            } else {
                //.....
            }
        },
        // 设置时间间隔
        handleTankTime(e) { //蒸罐所需时间
            const { value } = e.target
            let tankTime = this.changeTime(value)
            this.minutes30 = tankTime
            this.tankTime = value
            this.DepartureTimeVal = moment(Number(moment(this.SteamtankTimeVal).format('x')) + tankTime)
            this.triggerChange({ tankTime: this.tankTime })
            this.triggerChange({ departureTime: this.DepartureTimeVal })
        },
        handleLoadFinishTime(e) { //装货所需时间
            const { value } = e.target
            let loadFinishTime = this.changeTime(value)
            this.hour2 = loadFinishTime
            this.loadTime = Number(value)
            this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + loadFinishTime)
            this.triggerChange({ loadFinishTime: this.LoadFinishTimeVal })
            this.triggerChange({ loadTime: this.loadTime })
        },
        handleUnLoadFinishTime(e) { //卸货所需时间
            const { value } = e.target
            let unLoadFinishTime = this.changeTime(value)
            this.hour2 = unLoadFinishTime
            this.unLoadTime = Number(value)
            this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + unLoadFinishTime)
            this.triggerChange({ unLoadFinishTime: this.UnloadFinishTimeVal })
            this.triggerChange({ unLoadTime: this.unLoadTime })
        },
        handleNeedLoadAdressTime(e) { //到达装货地所需时间
            const { value } = e.target
            let unLoadNeedTime = this.changeTime(value)
            this.needLoadAdressTime = Number(value)
            this.LoadAddressTimeVal = moment(Number(moment(this.DepartureTimeVal).format('x')) + unLoadNeedTime)
            this.triggerChange({ loadAddressTime: this.LoadAddressTimeVal })
            this.triggerChange({ needLoadAdressTime: this.needLoadAdressTime })
        },
        handleroadFinishTime(e) { //路途所需时间
            const { value } = e.target
            let roadNeedTime = this.changeTime(value)
            this.roadTime = Number(value)
            this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + roadNeedTime)
            this.triggerChange({ unLoadAddressTime: this.UnloadAddressTimeVal })
            this.triggerChange({ roadTime: this.roadTime })
        },
        changeTime(val) {
            let value = Number(val)
            let oneHour = 3600000
            return value * oneHour
        },
        handleSteamtankTimeInput(_moment, dateString) {
            this.SteamtankTimeVal = _moment
            let steamTankTime = Number(moment(_moment).format('x'))
            this.getLoadTime(steamTankTime)
            this.triggerChange({ steamtankTime: this.SteamtankTimeVal })
        },
        handleDepartureTimeInput(_moment, dateString) {
            this.DepartureTimeVal = _moment
            let departureTime = Number(moment(_moment).format('x'))
            this.getDepartureTime(departureTime);
            this.triggerChange({ departureTime: this.DepartureTimeVal })
        },
        handleLoadAddressTimeInput(_moment, dateString) {
            this.LoadAddressTimeVal = _moment
            this.triggerChange({ loadAddressTime: this.LoadAddressTimeVal })
        },
        handleLoadFinishTimeInput(_moment, dateString) {
            this.LoadFinishTimeVal = _moment
            this.triggerChange({ loadFinishTime: this.LoadFinishTimeVal })
        },
        handleUnloadAddressTimeInput(_moment, dateString) {
            this.UnloadAddressTimeVal = _moment
            this.triggerChange({ unLoadAddressTime: this.UnloadAddressTimeVal })
        },
        handleUnloadFinishTimeInput(_moment, dateString) {
            this.UnloadFinishTimeVal = _moment
            this.triggerChange({ unLoadFinishTime: this.UnloadFinishTimeVal })
        },
        triggerChange(changedValue) {
            let output = Object.assign({
                steamtankTime: this.SteamtankTimeVal,
                departureTime: this.DepartureTimeVal,
                loadAddressTime: this.LoadAddressTimeVal,
                loadFinishTime: this.LoadFinishTimeVal,
                unLoadAddressTime: this.UnloadAddressTimeVal,
                unLoadFinishTime: this.UnloadFinishTimeVal,
                tankTime: this.tankTime,
                loadTime: this.loadTime,
                unLoadTime: this.unLoadTime,
                needLoadAdressTime: this.needLoadAdressTime,
                roadTime: this.roadTime,
                typeId: this.typeId
            }, changedValue)
            this.$emit('input', output)
            this.$emit('change', output)
        }
    }

代码比较繁琐,还未进行精简操作…凑合看吧

3、在相应地方进行引入

剩下的就该怎么玩,就怎么玩咯

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: vue-antd form组件封装全过程

本文链接: https://www.lsjlt.com/news/210820.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue-antd form组件封装全过程
    目录表单项–公用组件(vue-antd)1、废话不多说,先看具体效果2、组件代码如下3、在相应地方进行引入总结表单项–公用组件(vue-antd) 1、废话不...
    99+
    2023-05-17
    vue-antd form组件封装 form组件封装 vue-antd form组件
  • Vue使用v-model封装el-pagination组件的全过程
    使用v-model绑定分页信息对象,分页信息对象包括3个核心属性参数,分页事件直接绑定查询数据的方法,消除父组件的handleSizeChange和handleCurrentChan...
    99+
    2024-04-02
  • vue封装form表单组件拒绝重复写form表单
    目录前言核心思想:实现重点:表单双向绑定的方式有两种: 1.使用v-model进行双向绑定2.使用v-model的语法糖配置项整体字段:效果浏览源码放送1. baseFor...
    99+
    2024-04-02
  • vue中封装echarts公共组件过程
    目录1、安装echarts2、在mian.js中全局引入3、下面开始封装图表4、接下来只需要在需要显示图表的地方引入Echart.vue定义图表公共样式是为了统一同一网站各页面图表的...
    99+
    2024-04-02
  • 从零搭建react+ts组件库(封装antd)的详细过程
    目录整体需求开发与打包工具选型使用webpack作为打包工具使用babel来处理typescript代码使用less-loader、css-loader等处理样式代码项目搭建思路整体...
    99+
    2024-04-02
  • Vue中图片上传组件封装-antd的a-upload二次封装的实例
    目录图片上传组件封装-antd的a-upload二次封装api组件封装使用优化图片上传组件封装-antd的a-upload二次封装 a-upload组件 api const pub...
    99+
    2024-04-02
  • vue3递归组件封装的全过程记录
    目录前言1、递归组件2、右键菜单组件总结前言 今天在写项目时,遇到一个自定义右键菜单的需求。在菜单中还有子菜单,所以这个时候就要用到递归组件了。所以写下这篇文章来记录一下自己编写递归...
    99+
    2024-04-02
  • Vue如何封装全局toast组件
    本篇内容主要讲解“Vue如何封装全局toast组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue如何封装全局toast组件”吧!一. 借助 vue-cli...
    99+
    2024-04-02
  • vue二次封装一个高频可复用组件的全过程
    目录前言继承原有组件接口v-model插槽组件更抽象总结前言 在我们的业务里,我们通常会二次封装一些高频业务组件,比如弹框,抽屉,表单等这些业务组件,为什么要二次封装?我们所有人心里...
    99+
    2022-11-13
    vue如何二次封装组件 vue组件封装并复用 vue的组件复用机制
  • vue3+vite自定义封装vue组件发布到npm包的全过程
    目录创建项目创建组件导出组件使用vite构建打包注册->登录npm发布前准备发布到npm参考:总结创建项目 “vue”: “^3.2.8&r...
    99+
    2024-04-02
  • 怎么用Vue封装全局toast组件
    本篇内容介绍了“怎么用Vue封装全局toast组件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一. 借助...
    99+
    2024-04-02
  • vue extend+promise封装全局弹窗组件
    本文实例为大家分享了vue extend+promise封装全局弹窗组件的具体代码,供大家参考,具体内容如下 因为项目没有引入第三方UI库,所以所有的公共组件都需要自己封装现在需要一...
    99+
    2024-04-02
  • Element Plus组件Form表单Table表格二次封装的完整过程
    目录前言Form表单的封装简述正常的使用开始封装①开始封装②开始封装③开始封装④完整封装代码⑤配置项类型文件配置项文件form表单组件文件page-search组件文件role页面组...
    99+
    2024-04-02
  • Vue封装全局toast组件的应用
    这篇文章主要介绍“Vue封装全局toast组件的应用”,在日常操作中,相信很多人在Vue封装全局toast组件的应用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue封装全局toast组件的应用”的疑惑有所...
    99+
    2023-06-20
  • vue组件如何封装
    封装vue组件的方法:1.新建vue.js项目;2.使用Vue.extend()方法创建组件;3.使用Vue.component()方法注册组件;4.构建组件变量;5.使用组件名称标签调用组件;具体步骤如下:首先,在vue-cli中创建一个...
    99+
    2024-04-02
  • Vue封装全局toast组件的完整实例
    目录前言 一. 借助 vue-cli 1. 定义 Toast 组件2. 在 main.js 里面配置3. 在其他组件内使用二、不借助 vue-cli 1. 注册 toast 组件2....
    99+
    2024-04-02
  • vue实现全选组件封装实例详解
    效果  封装的组件 <template> <el-form-item :label="label"> <el-checkbox ...
    99+
    2024-04-02
  • vue 的全选组件封装你知道多少
    效果  封装的组件 <template> <el-form-item :label="label"> <el-checkbox ...
    99+
    2024-04-02
  • vue 为什么要封装全局组件引入
    目录1.为什么要封装全局组件引入2. 封装全局组件的 js3. 在 main.js 中4. 我们要引入这些公共组件就可以直接用啦1.为什么要封装全局组件引入 为何要封装一个封装全局组...
    99+
    2024-04-02
  • vue中全选组件封装的示例分析
    这篇文章将为大家详细讲解有关vue中全选组件封装的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果 封装的组件<template>  <el-fo...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作