upload上传图片

需求分析:
1.添加商品页面可以添加图片
2.编辑商品页面可以编辑图片(原来的图片列表上)

子组件 (ImageUpload.vue):

<template>
    <div>
        <div class="flex-img">
            <div class="flex-img__item" v-for="(image,index) of imageList" :key="index">
                <div>
                    <img class="flex-img__image" :src="image">
                </div>
                <i class="el-icon-error right-up" @click.stop="handleRemove(index)"></i>
            </div>

            <el-upload
                    :action="uploadUrl"
                    :show-file-list="false"
                    accept="image/jpeg,image/jpg,image/png"
                    list-type="picture-card"
                    :limit="imgNumber"
                    :on-success="handleSuccess"
                    :before-upload="beforeUpload">
                <i class="el-icon-plus"></i>
            </el-upload>
        </div>

        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过1M</div>
    </div>
</template>

<script>
    export default {
        props: {
            imageList: Array,
            clearList: Number,
            imgNumber: Number
        },
        data() {
            return {
                uploadUrl: 'http://xxxx.xxx.xx/api/upload',
                fileList: this.imageList ? this.imageList : [],
            };
        },
        watch: {
            imageList(value) {
                    this.fileList = value;
            },
            clearList() {
                this.fileList = [];
            },
            fileList(value) {
                this.$emit('update:imageList', value);
            }
        },
        methods: {
            handleRemove(index) {
                this.fileList.splice(index, 1);
            },
            handleSuccess(response) {
                this.fileList.push(response.url);
            },
            beforeUpload(file){
                const imageSize = file.size / 1024 / 1024 < 1;
                if(!imageSize){
                    this.$message.error('上传封面大小不能超过 1MB!');
                }
                return imageSize;
            }
        }
    };
</script>

<style scoped>
    .flex-img
    {
        display: flex;
    }
    .flex-img__item
    {
        position: relative;

        box-sizing: border-box;
        width: 148px;
        height: 148px;
        margin: 0 8px 8px 0;

        border: 1px solid #c0ccda;
        border-radius: 6px;
        background-color: #fff;
    }
    .right-up
    {
        position: absolute;
        z-index: 1024;
        top: -5px;
        right: -5px;
    }
    .flex-img__image
    {
        width: 146px;
        height: 146px;

        border-radius: 6px;
    }

</style>

父组件:

<template>
  <div>
     <image-upload :imageList.sync="imageShows" :clearList="clearList"></image-upload>
  </div>
</template>
<script>
import ImageUpload from '../../components/ImageUpload.vue';
export default{
  components:{
     ImageUpload
  },
  data(){
     clearList:null,//清除图片列表
     imageShows:[]//图片列表
  },
  methods:{
    resetForm(){
      this.clearList = Math.random();//给一个随机数就能清除
    }
  }
}
</script>

报错信息:app.js:1608 [Vue warn]: Error in callback for watcher “fileList”: “TypeError: Cannot create property ‘uid’ on string ‘https://xxx.jpeg‘”
app.js:2724 TypeError: Cannot create property ‘uid’ on string ‘https://xxx.jpeg
解决办法:子组件修改方法handleSuccess
handleSuccess(response) {
this.fileList.push({response.url});//多了 { },因为它要的是数组,那就传数组进去
},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值