当前位置:首页 > 经验笔记 > VUE > 正文内容

基于Vue的移动端图片裁剪组件(Clipic)可自动压缩

han32684年前 (2021-04-15)VUE2905

安装:

Bash
npm install --save clipic

使用代码:

Markup
<template>
    <div>
        <div class="photo">
            <img  :src="base64" />
            <input type="file" name="file" ref="files" accept="image/*" @change="uploadImg" />
        </div>
    </div>
</template>


<script>
import Clipic from 'clipic'
const clipic = new Clipic()
export default {
    components: {},
    data() {
        return {
            base64: '',
        }
    },
    methods: {
        uploadImg() {
            let _this = this
            const files = this.$refs.files.files
            const reader = new FileReader()
            reader.readAsDataURL(files[0])
            reader.onload = (img) => {
                clipic.getImage({
                    width: 200,
                    height: 200,
                    src: img.target.result,
                    onDone: (base64) => {
                        //这里就是上传完成的回调函数,可以在这里请求接口上传至服务器
                        _this.base64 = base64
                        console.log(this.base64) //图片上传完成后生成的base64
                    },
                })
            }
            //执行完成之后把input的value值置空,否则无法选择相同的图片
            this.$refs.files.value = ''
        },
    },
}
</script>

<style scoped lang="less">
.photo{
    width: 100px;
    img{
        width: 100%;
        
    }
}
</style>
<style  >
</style>

使用展示:

image.png

扫描二维码推送至手机访问。

版权声明:本文由瀚文博客发布,如需转载请注明出处。

本文链接:https://hanwenblog.com/post/53.html

分享给朋友:

相关文章

vue中实现点击按钮滚动到页面对应位置 使用css3平滑属性实现

vue中实现点击按钮滚动到页面对应位置 使用css3平滑属性实现

vue项目中,需要实现点击对应按钮,滚动到对应页面位置,下面分享一个简单实用的方法<template>   <div class="box&...

vue element table expand 设置只可以展开一行、设置点击行即可打开扩展内容

在Vue中使用Element UI的el-table组件时,‌可以通过以下步骤设置只可以展开一行以及通过点击行即可打开扩展内容:‌设置只可以展开一行:‌通过监听expand-change事件来实现每次...

vue中$set的使用踩坑日记

vue中$set的使用踩坑日记

1、为什么要用set?在vue中,并不是任何时候数据都是双向绑定的。在官方文档中,有这样一段话,如下:从文档得知,当数据没有被双向绑定的时候,我们就需要使用set了2、set用法解决数据没有被双向绑定...

vue强制更新$forceUpdate()

vue强制更新$forceUpdate()

vue强制更新$forceUpdate()添加this.$forceUpdate();进行强制渲染,效果实现。搜索资料得出结果:因为数据层次太多,render函数没有自动更新,需手动强制刷新。调用强制...

ElementUI的el-cascader级联选择器组件获取选中的label | VUE

ElementUI的el-cascader级联选择器组件获取选中的label | VUE

例如上图,需要拿到全部/设备:设备123<el-cascader   ref="myCascader"   :options=&q...