仿照el-upload 封装自己的上传控件(el-upload 移动端无法吊起相机)

input选择图片的那个选择在h5的时候在去年下半年突然无法无法出现唤醒相机的选项  不知道出现的原因 

发现el-upload作为h5的时候无法吊起相机 

又因为需要对服务端地址图片进行回显(处于编辑功能的情况下 非新增 新增el-upload 可以实现回显)  两个功能el-upload都不能很好的支持 所以自己仿照el-upload 做了上传组件的封装

具体代码如下图所示

<template>
    <div style="display: flex; height: 100%;">
        <div v-for="(item,idx) in httpLits" :key="item" class="listitem">
            <div class="inner">
                <img :src="`${item}`">
                <div class="delete" @click="delimg(idx)">
                    <i class="el-icon-delete"></i>
                </div>
            </div>
        </div>
        <div class="inputbox" v-if="httpLits.length !== 5" :class="httpLits.length > 0 ? 'listitem' : ''">
            <i class="ri-camera-fill"></i>
            <span>{{ httpLits.length === 0 ? '添加图片' : `还可添加${5 - httpLits.length}张`
            }}</span>
            <input v-if="inputshow" type="file" name="image" :accept="'image/*'" @change="onchangeImgFun"
                style="position: absolute; width: 100%;height: 100%;opacity: 0;" />
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import _ from "lodash"
export default {
    props: ['value'],
    data() {
        return {
            inputshow: true,
            httpLits: [],
        }
    },
    created() {
        this.httpLits=_.cloneDeep(Array.isArray(this.value) ?this.value:[])
      
       
    },
    methods: {
        onchangeImgFun(e) {
            console.log(e.target.files)
            var file = e.target.files[0];
            this.inputshow = false
            let formData = new FormData()
            formData.append('image', file)
            axios({
                method: 'post',
                url: `xxxx`,
                data: formData
            },

            ).then(res => {
                this.httpLits.push(res.data.data.image)
                this.$emit('input', this.httpLits)
                this.inputshow = true
            }).catch(() => {
                alert('上传失败')
            })
        },
        // 删除图片
        delimg(idx) {
            this.httpLits.splice(idx, 1)
            this.$emit('input', this.httpLits)
        },
    }

}
</script>

<style lang="scss" scoped>
.inputbox {
    height: 4.125rem;
    line-height: unset;
    background-color: #F2F6F8;
    width: calc(100vw - 1.75rem);
    border-radius: 6px;
    border-color: #f2f6f8;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    box-sizing: border-box;

    i {
        font-size: 24px;
        color: #4E5969;
    }

    span {
        font-weight: 400;
        font-size: 12px;
        color: #86909C;
    }
}

.listitem {
    width: calc(100% / 5);
    height: 100%;
    padding: 1px 4px;
    box-sizing: border-box;

    .inner {
        border-radius: 4px;
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;

        img {
            width: 100%;
            height: 100%;
        }

        .delete {
            position: absolute;
            right: 0;
            bottom: 0;
            width: 20px;
            height: 20px;
            z-index: 9999;
            background-color: #ffffff44;
            border-radius: 12%;
            display: flex;
            align-items: center;
            justify-content: center;

            i {
                color: #f53f3f;
                font-size: 12px;
            }
        }
    }

    span {
        font-weight: 400;
        font-size: 10px;
        color: #86909C;
    }


}
</style>

效果图如下:

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值