我是靠谱客的博主 追寻鸡,这篇文章主要介绍elementUI组件封装(一) ------ 图片上传,现在分享给大家,希望可以做个参考。

封装组件

复制代码
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<template> <div> <!-- 新增 --> <div v-if="!id" class="imgList-uploader" > <el-upload class="avatar-uploader" :action="domain" :data="QiniuData" :on-remove="handleRemove" :on-error="uploadError" :on-success="uploadSuccess" :before-remove="beforeRemove" :before-upload="beforeUpload" :on-exceed="handleExceed" :show-file-list="false" > <img v-if="image" :src="image" alt /> <i v-else class="el-icon-plus avatar-uploader-icon" ></i> </el-upload> </div> <!-- 修改 --> <div class="form-img" v-else > <!-- 有图的修改 --> <img v-if="image && image.length>0" :src="image" @click="innerVisible = true" /> <!-- 无图的修改 --> <div v-else class="imgList-uploader" > <el-upload class="avatar-uploader" :action="domain" :data="QiniuData" :on-remove="handleRemove" :on-error="uploadError" :on-success="uploadSuccess" :before-remove="beforeRemove" :before-upload="beforeUpload" :on-exceed="handleExceed" :show-file-list="false" > <img v-if="image" :src="image" alt /> <i v-else class="el-icon-plus avatar-uploader-icon" ></i> </el-upload> </div> </div> <el-input class="bg-inputWrap" v-model="image" @change="handleInputChange" ></el-input> <!-- 修改图片弹窗 --> <el-dialog append-to-body :visible.sync="innerVisible" title="修改图片" > <div :class="isImgRectangle ? 'rectangleImg' : 'squareImg'"> <img style="width:100%;height:100%;" :src="image" /> </div> <span slot="footer" class="dialog-footer" > <el-upload style="display:inline-block;vertical-align:middle" :action="domain" :data="QiniuData" :on-remove="handleRemove" :on-error="uploadError" :on-success="uploadSuccess" :before-remove="beforeRemove" :before-upload="beforeUpload" :on-exceed="handleExceed" :show-file-list="false" > <el-button>上传图片</el-button> </el-upload> <el-button style="margin-left:10px" type="primary" @click="innerVisible = false" >确定</el-button> </span> </el-dialog> </div> </template> <script> export default { props: { id: { type: Number }, parentImage: { type: String }, isImgRectangle: { type: Boolean, default: true } }, data() { return { image: "", innerVisible: false, QiniuData: { key: "", //图片名字处理 token: "" //七牛云token }, domain: "http://***.com", // 七牛云的上传地址 qiniuaddr: "http://***.com" // 七牛云的图片外链地址 }; }, created() { if (this.id == 1) { this.image = this.parentImage; } else { this.image = ""; } this.getQiniuToken(); }, methods: { handleInputChange(val) { this.$emit("oneImage", val); }, handleRemove(file, fileList) { this.image = ""; }, handleExceed(files, fileList) { this.$message.warning( `当前限制选择 1 张图片,如需更换,请删除上一张图片在重新选择!` ); }, beforeUpload(file) { const isPNG = file.type === "image/png"; const isJPEG = file.type === "image/jpeg"; const isJPG = file.type === "image/jpg"; const isLt2M = file.size / 1024 / 1024 < 2; if (!isPNG && !isJPEG && !isJPG) { this.$message.error("上传图片只能是 jpg、png、jpeg 格式!"); return false; } if (!isLt2M) { this.$message.error("上传图片大小不能超过 2MB!"); return false; } var index1 = file.name.lastIndexOf("."); var index2 = file.name.length; var suffix = file.name.substring(index1 + 1, index2); //时间戳 var timestamp = Date.parse(new Date()); this.QiniuData.key = `upload_pic_${timestamp}.${suffix}`; }, uploadSuccess(response, file, fileList) { this.image = `${this.qiniuaddr}/${response.key}`; this.$emit("oneImage", this.image); }, uploadError(err, file, fileList) { this.$message({ message: "上传出错,请重试!", type: "error", center: true }); }, beforeRemove(file, fileList) { // return this.$confirm(`确定移除 ${ file.name }?`); }, getQiniuToken() { this.$http({ url: this.$http.adornUrl("/lesson/file/qiniu-token"), method: "get" }) .then(res => { this.QiniuData.token = res.data.token; }) .catch(error => {}); } } }; </script> <style lang="scss" scoped> .avatar-uploader-icon { width: 180px !important; height: 180px !important; line-height: 180px !important; } .squareImg { width: 360px; height: 360px; margin: 0 auto; } .rectangleImg { width: 640px; height: 360px; margin: 0 auto; } </style>

使用方式

html

复制代码
1
2
3
4
5
6
7
8
9
<template> <image-upload-vue v-if="testShow" :id="dataForm.id == 0 ? 0 : 1" :parentImage="dataForm.bannerImage" @oneImage="oneImageFun" ></image-upload-vue> </template>

js

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import imageUploadVue from "@/components/image-upload/image-upload.vue"; components:{ imageUploadVue } data(){ return { dataForm:{ id:0, bannerImage:"" } } }, methods(){ // 单图上传 oneImageFun(val) { this.dataForm.bannerImage = val; }, }

最后

以上就是追寻鸡最近收集整理的关于elementUI组件封装(一) ------ 图片上传的全部内容,更多相关elementUI组件封装(一)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部