Vue開(kāi)發(fā)之封裝上傳文件組件與用法示例
本文實(shí)例講述了Vue開(kāi)發(fā)之封裝上傳文件組件與用法。分享給大家供大家參考,具體如下:
使用elementui的 el-upload插件實(shí)現(xiàn)圖片上傳組件
每個(gè)項(xiàng)目存在一定的特殊性,所以數(shù)據(jù)的處理會(huì)不同

pictureupload.vue:
<template>
<div class="pictureupload">
<el-upload
:action="baseUrl + '/api/public/image'"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:file-list="fileList"
:on-exceed="handleExceed"
:before-remove="beforeRemove"
name="img"
:on-success="upLoadSuccess"
:data="upLoadData"
:headers="headers"
:limit="limit">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
import store from "@/store";
export default {
props: {
imgList: {
type: Array,
default: []
}, // 父組件傳遞的圖片列表
limit: "" // 圖片數(shù)量限制
},
data() {
return {
fileList: [],
upLoadData: {
img: ""
},
baseUrl: process.env.BASE_API,
dialogVisible: false,
dialogImageUrl: "",
headers: {
Authorization: store.getters.token_type + " " + store.getters.token
} // 接口調(diào)用token
};
},
watch: {
// 監(jiān)聽(tīng)imgList的變化: fileList要求的格式為[{url: img}],所以監(jiān)聽(tīng)imgList變化后將其進(jìn)行處理,賦值給fileList
imgList: {
handler(newValue, oldValue) {
if(newValue.length === 0) {
this.fileList = [];
return;
}
for (let i = 0; i < newValue.length; i++) {
if (oldValue[i] != newValue[i]) {
this.fileList = [];
newValue.forEach(el => {
this.fileList.push({url: el})
})
return;
}
}
},
deep: true
}
},
methods: {
// 圖片移除時(shí)處理數(shù)據(jù)
handleRemove(file, fileList) {
let item = [];
fileList.forEach(el => {
item.push(el.url);
});
this.$emit("removeimg", item);
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
// 判斷圖片數(shù)量
handleExceed(files, fileList) {
this.$message.warning(`當(dāng)前限制選擇 ${this.limit} 個(gè)文件,本次選擇了 ${files.length} 個(gè)文件,共選擇了 ${files.length + fileList.length} 個(gè)文件`);
},
beforeRemove(file, fileList) {},
// 上傳圖片成功事件
upLoadSuccess(response) {
this.$emit("uploadimg", response.message);
this.$message("上傳成功",);
}
},
mounted() {
if (this.imgList.length != 0) {
this.imgList.forEach(el => {
this.fileList.push({ url: el });
});
}
}
};
</script>
使用上傳圖片組件:
<pictureupload @uploadimg="uploadimg" :imgList="ruleForm.img" :limit="3" @removeimg="removeimg"></pictureupload>
removeimg(item) {
this.ruleForm.img = item;
},
uploadimg(item) {
this.ruleForm.img.push(item);
},
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
- Vue如何實(shí)現(xiàn)文件預(yù)覽和下載功能的前端上傳組件
- Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能
- 利用Vue3+Element-plus實(shí)現(xiàn)大文件分片上傳組件
- vue2中基于vue-simple-upload實(shí)現(xiàn)文件分片上傳組件功能
- vue3.0搭配.net core實(shí)現(xiàn)文件上傳組件
- Vue封裝一個(gè)簡(jiǎn)單輕量的上傳文件組件的示例
- vue webuploader 文件上傳組件開(kāi)發(fā)
- Vue 中自定義文件上傳組件的實(shí)現(xiàn)代碼
相關(guān)文章
vue實(shí)現(xiàn)滾動(dòng)鼠標(biāo)滾輪切換頁(yè)面
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滾動(dòng)鼠標(biāo)滾輪切換頁(yè)面,類似于縱向走馬燈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Vue.js實(shí)現(xiàn)一個(gè)todo-list的上移下移刪除功能
這篇文章主要介紹了Vue.js實(shí)現(xiàn)一個(gè)todo-list的上移下移刪除功能,需要的朋友可以參考下2017-06-06
vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue3中實(shí)現(xiàn)過(guò)渡動(dòng)畫的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Vue3中實(shí)現(xiàn)過(guò)渡動(dòng)畫的一些常用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10
詳解vue表單驗(yàn)證組件 v-verify-plugin
本篇文章主要介紹了詳解vue表單驗(yàn)證組件 v-verify-plugin,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Vue同一路由強(qiáng)制刷新頁(yè)面的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了解決VUE同一路由強(qiáng)制刷新頁(yè)面的問(wèn)題,本文給大家分享實(shí)現(xiàn)過(guò)程,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
vue源碼解讀子節(jié)點(diǎn)優(yōu)化更新
這篇文章主要為大家介紹了vue源碼解讀子節(jié)點(diǎn)優(yōu)化更新示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

