vue element upload組件 file-list的動(dòng)態(tài)綁定實(shí)現(xiàn)
本文解決,upload組件 file-list的動(dòng)態(tài)綁定list1,list2 ...,實(shí)現(xiàn)動(dòng)態(tài)添加,相信很多電商后臺(tái)管理系統(tǒng)都會(huì)遇到這個(gè)需求,例子如下
本例,我是使用的upload默認(rèn)的上傳地址(很多圖片不能上傳,你可以在本地截幾張圖片,進(jìn)行測(cè)試),我可以上傳多張活動(dòng)圖片,可以加相應(yīng)的,名稱,鏈接描述等,如果有多個(gè)活動(dòng),可以點(diǎn)擊添加活動(dòng),在第二個(gè)活動(dòng)又能添加相應(yīng)的內(nèi)容,保存完之后,可以實(shí)現(xiàn)回顯,活動(dòng)詳情頁(yè)可以看到添加的幾個(gè)活動(dòng)和相應(yīng)的活動(dòng)內(nèi)容。
實(shí)現(xiàn)方法不為一,這是一種特別簡(jiǎn)單的。代碼如下
<template> <div class="view"> <div class="item" v-for="(item,index) in formList" :key="index"> <div style="font-size: 14px; color: #606266;line-height: 40px;">相關(guān)圖片資料</div> <el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="(file, fileList)=>{return handleRemove(file, fileList, index)}" :limit="5" :on-exceed="onExceed" :file-list="item.pics" :on-success="(response, file, fileList)=>{return onSuccess(response, file, fileList, index)}" > <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt /> </el-dialog> <el-form label-position="top" label-width="80px" :model="item"> <el-row :gutter="20"> <el-col :span="12"> <el-form-item label="活動(dòng)名稱"> <el-input v-model="item.name"></el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="活動(dòng)鏈接"> <el-input v-model="item.content"></el-input> </el-form-item> </el-col> </el-row> </el-form> <el-button type="danger" @click="delItem(index)" style="margin-bottom:20px">刪除</el-button> </div> <el-button type="success" @click="addItem" style="width:1000px">添加活動(dòng)</el-button> <el-button type="primary" @click="saveItem" style="margin-top:20px;margin-left:0;">保存活動(dòng)</el-button> </div> </template> <script> export default { name: "HelloWorld", data() { return { dialogImageUrl: "", dialogVisible: false, formList: [{ pics: [] }] }; }, methods: { // 上傳圖片 onSuccess(response, file, fileList, idx) { // 這里是element的上傳地址,對(duì)應(yīng)的name,url,自己打印fileList對(duì)照 this.formList[idx].pics.push({ name: file.name, url: file.url }); }, // 移除圖片 handleRemove(file, fileList, idx) { let Pics = this.formList[idx].pics; Pics.forEach((item, index) => { if (file.name == item.name) { Pics.splice(index, 1); } }); }, // 查看圖片 handlePictureCardPreview(file) { this.dialogImageUrl = file.url; this.dialogVisible = true; }, onExceed(file, fileList) { this.$message({ type: "warning", message: "最多上傳5張" }); }, // 添加活動(dòng) addItem() { this.formList.push({ pics: [] }); }, // 刪除活動(dòng) delItem(idx) { if (this.formList.length > 1) { this.formList.splice(idx, 1); } else this.formList = [{ pics: [] }]; }, // 保存活動(dòng) saveItem() { this.$message({ type: "success", message: "保存成功,可以刷新下試試回顯效果" }); console.log(this.formList); localStorage.setItem("formList", JSON.stringify(this.formList)); } }, created() { this.formList = JSON.parse(localStorage.getItem("formList"))|| [ { pics: [] } ];; } }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .view { width: 1000px; margin: 0 auto; } .item { width: 100%; } </style>
主要實(shí)現(xiàn),動(dòng)態(tài)添加多個(gè)item,每個(gè)item都有對(duì)應(yīng)的多張圖文和介紹,可以刪除,保存,下次進(jìn)來(lái)可以回顯繼續(xù)編輯,詳情展示頁(yè)可以展示
github地址,可以clone下來(lái),本地跑一下看看
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09element-ui中頁(yè)面縮放時(shí)table表格內(nèi)容錯(cuò)位的解決
這篇文章主要介紹了element-ui中頁(yè)面縮放時(shí)table表格內(nèi)容錯(cuò)位的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue3學(xué)習(xí)筆記簡(jiǎn)單封裝axios示例實(shí)現(xiàn)
這篇文章主要為大家介紹了vue3學(xué)習(xí)筆記簡(jiǎn)單封裝axios示例實(shí)現(xiàn),2022-06-06ElementUI中利用table表格自定義表頭Tooltip文字提示
這篇文章主要介紹了ElementUI中利用table表格自定義表頭Tooltip文字提示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)把頁(yè)面導(dǎo)出成word文件的方法,文中的實(shí)現(xiàn)步驟講解詳細(xì),并且有詳細(xì)的代碼示例,需要的小伙伴可以參考一下2023-10-10