zl程序教程

您现在的位置是:首页 >  后端

当前栏目

若依管理后台多图片上传组件

后台上传组件 管理 图片 若依
2023-09-11 14:20:10 时间

原文链接:https://blog.csdn.net/wgh0315/article/details/115858872

组件路径


组件 index.vue 详情
<template>
<div class="component-upload-image">
<el-upload
:action="uploadImgUrl"
list-type="picture-card"
:on-success="handleUploadSuccess"
:before-upload="handleBeforeUpload"
:on-error="handleUploadError"
name="file"
:on-remove="removeImage"
:show-file-list="true"
:headers="headers"
style="display: inline-block; vertical-align: top"
:file-list="photos"
>
<el-image v-if="!value" :src="value">
<div slot="error" class="image-slot"><i class="el-icon-plus" /></div>
</el-image>
<div v-else class="image">
<el-image :src="value" :style="`width:100%;height:100%;`" />
<div class="mask">
<div class="actions">
<span title="预览" @click.stop="dialogVisible = true"><i class="el-icon-zoom-in" /></span>
<span title="移除" @click.stop="removeImage(value)"><i class="el-icon-delete" /></span>
</div>
</div>
</div>
</el-upload>
<el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body><img :src="value" style="display: block; max-width: 100%; margin: 0 auto;" /></el-dialog>
</div>
</template>

<script>
import { getToken } from '@/utils/auth';

export default {
data() {
return {
dialogVisible: false,
uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
headers: {
Authorization: 'Bearer ' + getToken()
},
photos: []
};
},
props: {
value: {
type: String,
default: ''
},
urls: {
type: String,
default: ''
}
},
methods: {
removeImage(res) {
this.$emit('removeImage', res.url);
},
handleUploadSuccess(res) {
this.$emit('input', res.url);
this.loading.close();
},
handleBeforeUpload() {
this.loading = this.$loading({
lock: true,
text: '上传中',
background: 'rgba(0, 0, 0, 0.7)'
});
},
handleUploadError() {
this.$message({
type: 'error',
message: '上传失败'
});
this.loading.close();
},
getImgs(arr){
this.photos = []
if(arr) arr = arr.split(',');
for (var i in arr) {
this.photos.push({
uid: i,
url: arr[i]
});
}
}
},
mounted() {
this.getImgs(this.urls)
},
watch: {
urls(arr) {
if(arr){
this.getImgs(arr)
}
}
}
};
</script>

<style scoped lang="scss">
.image {
position: relative;
.mask {
opacity: 0;
position: absolute;
top: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.5);
transition: all 0.3s;
}
&:hover .mask {
opacity: 1;
}
}
</style>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
引用组件页面
<!--这是要上传多图片的字段-->
<el-form-item label="多图片上传">
<imageUpload @removeImage="removeImage" :urls="form.photos" @input="geturl"/>
</el-form-item>


import ImageUpload from '@/components/moreImgUpload';//引用组件
export default {
components: {
ImageUpload,
},
data() {
return {
photos:[],
};
},
methods: {
removeImage(value){
if(value){
this.form.photos = this.form.photos.replace(this.form.photos.indexOf(','+value)==-1?value+',':','+value,'')
}

},
geturl(url){
if (this.form.photos){
this.form.photos=this.form.photos+(','+url);
}else {
this.form.photos=url;
}
},
/** 提交按钮 */
submitForm() {
this.$refs["form"].validate(valid => {
console.log(this.form.photos.split(','));
});
},
}
}