Vue開發(fā)之封裝上傳文件組件與用法示例
本文實(shí)例講述了Vue開發(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)聽imgList的變化: fileList要求的格式為[{url: img}],所以監(jiān)聽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 文件上傳組件開發(fā)
- Vue 中自定義文件上傳組件的實(shí)現(xiàn)代碼
相關(guān)文章
Vue項(xiàng)目引進(jìn)ElementUI組件的方法
這篇文章主要介紹了Vue項(xiàng)目引進(jìn)ElementUI組件的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11
web前端Vue報(bào)錯(cuò):Uncaught?(in?promise)?TypeError:Cannot?read?
這篇文章主要給大家介紹了關(guān)于web前端Vue報(bào)錯(cuò):Uncaught?(in?promise)?TypeError:Cannot?read?properties?of?nu的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Vue動(dòng)態(tài)引用json數(shù)據(jù)的兩種方式
在 Vue 項(xiàng)目中引用 JSON 文件非常簡(jiǎn)單,尤其是當(dāng)文件內(nèi)容是一個(gè)數(shù)組時(shí),本文給大家介紹了Vue動(dòng)態(tài)引用json數(shù)據(jù)的兩種方式,并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2025-04-04
Vue proxyTable配置多個(gè)接口地址,解決跨域的問題
這篇文章主要介紹了Vue proxyTable配置多個(gè)接口地址,解決跨域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue項(xiàng)目base64字符串轉(zhuǎn)圖片的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

