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

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

han32685年前 (2021-04-15)VUE3384

安装:

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的移动端图片裁剪组件(vue-imgcut)

基于Vue的移动端图片裁剪组件(vue-imgcut)

安装:npm install vue-imgcut –save使用代码:<template>     <div...

element中el-switch开关点击弹出确认框 阻止默认的el-switch 的点击事件 发送请求成功后才改变状态

element中el-switch开关点击弹出确认框 阻止默认的el-switch 的点击事件 发送请求成功后才改变状态

Switch 开关表示两种相互对立的状态间的切换,多用于触发「开/关」。常见用法: 1、绑定v-model到一个Boolean类型的变量。可以使用active-color属性与inactiv...

VUE拖放排序,好用的vue拖拽插件vuedraggable拖放排序

VUE拖放排序,好用的vue拖拽插件vuedraggable拖放排序

1  安装依赖:npm i -S vuedraggable2. 使用组件:html:    <draggable&...

vue 去除前后空格trim的使用方法

一、使用trim修饰符<input v-model.trim = "massage" >二、使用filter过去属性 <...

vue跳转时打开新窗口的方法

    1、<vue-link>标签实现新窗口打开    官方文档中说 v-link 指令被 <rou...