美文网首页
小程序上传图片

小程序上传图片

作者: 名字很容易想 | 来源:发表于2019-01-20 14:47 被阅读0次

    参考:https://www.jianshu.com/p/c1e0574ee63d
    或者参考:https://www.cnblogs.com/changyaoself/p/9596237.html (mpvue,图片上传预览删除)

    <template>
    <div class="j-pic-upload">
    <div class="j-upload-btn" @click="uploadImg()" :style="{'width':width || '120rpx','height':height || '120rpx'}">
    <span class="j-upload-add">+</span>
    </div>
    <img @click="previewImg(index)" v-for="(src,index) in urls" :key="src" :src="src" :style="{'width':width || '120rpx','height':height || '120rpx'}" class="img" >
    </div>
    </template>

    <script>
    export default {
    props:["width","height","max","srcs"],
    data(){
    return {
    urls:[]
    }
    },
    mounted(){
    this.urls = this.srcs || [];
    },
    methods:{
    uploadImg(){
    let that = this;
    wx.chooseImage({
    count: that.max || 3,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success: function (res) {
    res.tempFilePaths.forEach(v=>{
    that.urls.push(v);
    });
    that.emit("choosed",{all:that.urls,currentUpload:res.tempFilePaths}); } }) }, previewImg(index){ let that = this; wx.showActionSheet({ itemList:["预览","删除"], success: function(res) { if(res.tapIndex === 0){ wx.previewImage({ current:that.urls[index], urls:that.urls }); } else { that.urls.splice(index,1); that.emit("delete",that.urls);
    }
    },
    });
    },
    }
    }
    </script>

    <style scoped>
    .j-pic-upload{
    padding: 10rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    }
    .j-upload-btn{
    border: 1px dashed #ddd;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    margin-right: 20rpx;
    }
    .j-upload-add{
    font-size: 80rpx;
    font-weight: 500;
    color:#C9C9C9;
    }
    .img{
    margin:10rpx 20rpx 10rpx 0;
    }
    </style>

    相关文章

      网友评论

          本文标题:小程序上传图片

          本文链接:https://www.haomeiwen.com/subject/wzpxjqtx.html