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

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

han32684年前 (2021-04-15)VUE2684

安装:

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

分享给朋友:

相关文章

elementUI Cascader组件在for循环中无法使用getCheckedNodes这个方法

elementUI Cascader组件在for循环中无法使用getCheckedNodes这个方法

上一篇文章说了elementUI组件获取label的值,把Cascader选择器放在for循环中问题又来了用getCheckedNodes报错没有这个方法我就试探性的看看他的上级有没有存储 ...

VUE computed和watch的区别最简化

VUE computed和watch的区别最简化

<!DOCTYPE html> <html lang="en"> <head>    &...

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

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

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

vue 弹框使用this.$emit调用父组件方法及传参 无效 (已解决) this.$parent

// 在子组件中调用父组件的method1方法 this.$parent.method1() // 获取父组件属性值 this.$parent.prop...

vue-cli3.x不同环境打包运行不同api接口及打包后动态配置API地址

vue-cli3.x不同环境打包运行不同api接口及打包后动态配置API地址

今天接到一个测试反馈说“我能不能我直接打包测试环境项目不用手动去改API”我第一反应就是这个测试这么懒!改个API都嫌麻烦,但是仔细一想毕竟"懒惰使人进步"嘛! &nbs...