element上傳函數帶參數,自定義傳參

下面這是標籤
可以看出來 :http-request="(params) =>beforeMasterPictureUpload(params,‘ruleForm’,fileList0)"
http-request 傳遞參數的方法 是可以這樣寫的,其他方法一樣,但是如果寫錯少些可能會導致覆蓋原來的方法。

<el-upload :class="{hidesse:hideUpload0,'avatar-uploader':true}" 
						multiple action="這裏不需要填或者隨便寫"
                        list-type="picture-card" :before-upload="beforeAvatarUpload"
                        :on-preview="(params) =>handlePictureCardPreview(params,'ruleForm',fileList0)"
                        :http-request="(params) =>beforeMasterPictureUpload(params,'ruleForm',fileList0)"
                        :on-change="(params,fileList) =>onChangeMaster(params,fileList,'ruleForm','fileList0')"
                        :on-exceed="OnExceed"
                        :on-remove="(params) =>handleRemove(params,'ruleForm',fileList0,'fileList0')" :limit="5"
                        :file-list="fileList0" @click="titext">
                        <i class="el-icon-plus"></i>
                    </el-upload>

下面是JS 接受方法 同樣 我們拿 http-request 這個方法做例子

beforeMasterPictureUpload(params, fromData, prop) {
	console.log('----aaaa', this.fileList);
    console.log('file', params, fromData, prop);
}

大家可以輸出看看結果 是否有拿到你上面傳遞過來的字符串或者是值

下面我將 源碼放上
HTML標籤

<el-upload :class="{hidesse:hideUpload0,'avatar-uploader':true}" 
						multiple action="這裏不需要填或者隨便寫"
                        list-type="picture-card" :before-upload="beforeAvatarUpload"
                        :on-preview="(params) =>handlePictureCardPreview(params,'ruleForm',fileList0)"
                        :http-request="(params) =>beforeMasterPictureUpload(params,'ruleForm',fileList0)"
                        :on-change="(params,fileList) =>onChangeMaster(params,fileList,'ruleForm','fileList0')"
                        :on-exceed="OnExceed"
                        :on-remove="(params) =>handleRemove(params,'ruleForm',fileList0,'fileList0')" :limit="5"
                        :file-list="fileList0" @click="titext">
                        <i class="el-icon-plus"></i>
                    </el-upload>

JS語句

// 主圖上傳之前
                beforeAvatarUpload(file) {
                    console.log('主圖上傳之前:', file)
                    const idJPG =
                        file.type === "image/jpeg" || "image/gif" || "image/png" || "image/bmp";
                    if (!idJPG) {
                        this.$message.error(
                            '123123'
                        );
                    }
                    return idJPG;
                },
                // 圖片觸發
                onChangeMaster(file, fileList, fromData, prop) {
                    // fileList 當前上傳框的數據 
                    switch (prop) {
                        case 'fileList0':
                            { this.hideUpload0 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList1':
                            { this.hideUpload1 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList2':
                            { this.hideUpload2 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList3':
                            { this.hideUpload3 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList4':
                            { this.hideUpload4 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList5':
                            { this.hideUpload6 = fileList.length >= this.limitCount; }
                            break;

                        default:
                            break;
                    }
                    console.log('判斷一次', file, fileList, this.fileList0)

                    if (this.inde > 0) {
                        return
                    } else {

                        //let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name)
                        //if (existFile) {
                           // this.inde++
                          //  console.log(existFile)
                          //  this.$message.error(
                          //      '圖片重複' + existFile.name
                        //    );
                        //    // fileList.pop()
                     //   }
                        this[prop] = fileList
                    }

                },
                // 圖片個數超出限制
                OnExceed(file, fileList) {
                    this.$message.error(
                        '每個選項最多上傳5張', "error"
                    );
                },
                handleRemove(params, fileList, prop, item) {
                    prop.forEach((obj, index) => {
                        console.log(index, obj)
                        if (params.uid == obj.uid) {
                            prop.splice(index, 1)
                        }

                    })
                    console.log('刪除後的數組', prop);
                    setTimeout(() => {
                        switch (item) {
                            case 'fileList0':
                                { this.hideUpload0 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList1':
                                { this.hideUpload1 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList2':
                                { this.hideUpload2 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList3':
                                { this.hideUpload3 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList4':
                                { this.hideUpload4 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList5':
                                { this.hideUpload6 = prop.length >= this.limitCount; }
                                break;

                            default:
                                break;
                        }
                        console.log(prop.length >= this.limitCount)
                    }, 1000)

                    // this.fileList = [];
                    this.dialogImageUrl = '';
                },
                handlePictureCardPreview(file, fromData, prop) {
                    console.log(file, fromData, prop)
                    this.dialogImageUrl = file.url;
                    this.dialogVisible = true;
                },
                beforeMasterPictureUpload(params, fromData, prop) {
                    console.log('----aaaa', this.fileList);
                    console.log('file', params, fromData, prop);
                    
                },

下面是需求案列
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章