vue element upload組件 file-list的動態(tài)綁定實(shí)現(xiàn)
本文解決,upload組件 file-list的動態(tài)綁定list1,list2 ...,實(shí)現(xiàn)動態(tài)添加,相信很多電商后臺管理系統(tǒng)都會遇到這個需求,例子如下

本例,我是使用的upload默認(rèn)的上傳地址(很多圖片不能上傳,你可以在本地截幾張圖片,進(jìn)行測試),我可以上傳多張活動圖片,可以加相應(yīng)的,名稱,鏈接描述等,如果有多個活動,可以點(diǎn)擊添加活動,在第二個活動又能添加相應(yīng)的內(nèi)容,保存完之后,可以實(shí)現(xiàn)回顯,活動詳情頁可以看到添加的幾個活動和相應(yīng)的活動內(nèi)容。
實(shí)現(xià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="活動名稱">
<el-input v-model="item.name"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="活動鏈接">
<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">添加活動</el-button>
<el-button type="primary" @click="saveItem" style="margin-top:20px;margin-left:0;">保存活動</el-button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
dialogImageUrl: "",
dialogVisible: false,
formList: [{ pics: [] }]
};
},
methods: {
// 上傳圖片
onSuccess(response, file, fileList, idx) {
// 這里是element的上傳地址,對應(yīng)的name,url,自己打印fileList對照
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張"
});
},
// 添加活動
addItem() {
this.formList.push({ pics: [] });
},
// 刪除活動
delItem(idx) {
if (this.formList.length > 1) {
this.formList.splice(idx, 1);
} else this.formList = [{ pics: [] }];
},
// 保存活動
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),動態(tài)添加多個item,每個item都有對應(yīng)的多張圖文和介紹,可以刪除,保存,下次進(jìn)來可以回顯繼續(xù)編輯,詳情展示頁可以展示
github地址,可以clone下來,本地跑一下看看
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)登陸注冊效果
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)登陸注冊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
element-ui中頁面縮放時table表格內(nèi)容錯位的解決
這篇文章主要介紹了element-ui中頁面縮放時table表格內(nèi)容錯位的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue3學(xué)習(xí)筆記簡單封裝axios示例實(shí)現(xiàn)
這篇文章主要為大家介紹了vue3學(xué)習(xí)筆記簡單封裝axios示例實(shí)現(xiàn),2022-06-06
ElementUI中利用table表格自定義表頭Tooltip文字提示
這篇文章主要介紹了ElementUI中利用table表格自定義表頭Tooltip文字提示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
vue實(shí)現(xiàn)把頁面導(dǎo)出成word文件的方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)把頁面導(dǎo)出成word文件的方法,文中的實(shí)現(xiàn)步驟講解詳細(xì),并且有詳細(xì)的代碼示例,需要的小伙伴可以參考一下2023-10-10

