elementUI 구성 요소 캡슐화 (3) ------- 멀티플렉스 업로드
49202 단어 element-UIvuejs
<template>
<div @click="aaa">
<div
class="weekImg"
v-for="(e,index) in numList"
:key="index"
>
<div class="weekTitle">{{e}}</div>
<div>
<!-- -->
<div v-if="id == 0">
<el-upload
class="avatar-uploader"
:action="domain"
:data="QiniuData"
:on-remove="handleRemove"
:on-error="uploadError"
:on-success="(res)=>{ return uploadSuccess(res, index)}"
:before-remove="beforeRemove"
:before-upload="beforeUpload"
:on-exceed="handleExceed"
:show-file-list="false"
>
<div
class="bg-img"
v-if="images[index]"
>
<img :src="images[index]" />
</div>
<i
v-else
class="el-icon-plus avatar-uploader-icon"
></i>
</el-upload>
</div>
<!-- -->
<div
class="bg-img"
v-else
>
<img
v-if="images[index]"
:src="images[index]"
@click="openModify(index)"
/>
<div v-else>
<el-upload
class="avatar-uploader"
:action="domain"
:data="QiniuData"
:on-remove="handleRemove"
:on-error="uploadError"
:on-success="(res)=>{ return uploadSuccess(res, index)}"
:before-remove="beforeRemove"
:before-upload="beforeUpload"
:on-exceed="handleExceed"
:show-file-list="false"
>
<div
class="bg-img"
v-if="images[index]"
>
<img :src="images[index]" />
</div>
<i
v-else
class="el-icon-plus avatar-uploader-icon"
></i>
</el-upload>
</div>
</div>
</div>
<div class="bg-inputWrap">
<el-input
@change="changeImg(index)"
v-model="imgValue[index]"
></el-input>
</div>
</div>
<el-dialog
:modal="false"
:visible.sync="shareVisible"
title=" "
>
<div :class="isImgRectangle ? 'rectangleImg' : 'squareImg'">
<img
style="width:100%;height:100%"
:src="images[modifyIndex]"
/>
</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="(res)=>{ return uploadSuccess(res, modifyIndex)}"
:before-remove="beforeRemove"
:before-upload="beforeUpload"
:on-exceed="handleExceed"
:show-file-list="false"
>
<el-button> </el-button>
</el-upload>
<!-- <el-button
type="primary"
@click="shareVisible=false"
> </el-button> -->
</span>
</el-dialog>
</div>
</template>
<script>
export default {
props: {
id: { type: Number },
parentImages: { type: String },
parentimgValue: { type: String },
imgTotal: { type: Number, default: 4 },
isImgRectangle: { type: Boolean, default: true }
},
data() {
return {
shareVisible: false,
modifyIndex: 0,
images: [], //
imgValue: [], //
numList: [],
QiniuData: {
key: "", //
token: "" // token
},
domain: "http://***.com", //
qiniuaddr: "http://***.com" //
};
},
created() {
if (this.id === 1) {
this.images = this.parentImages.split(" ");
this.imgValue = this.parentimgValue.split(" ");
}
if (this.imgTotal) {
for (let i = 0; i < this.imgTotal; i++) {
this.numList.push(` ${i + 1} `);
}
}
this.getQiniuToken();
},
methods: {
aaa(){
console.log('imags',this.images)
},
//
changeImg(index) {
this.$set(this.images, index, this.imgValue[index]);
let str = this.images.join(" ");
this.$emit("moreImages", str);
},
// ,
openModify(index) {
this.shareVisible = true;
this.modifyIndex = index;
},
handleRemove(file, fileList) {
// ,
// this.images = [];
},
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;
let self = this;
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 index2 = file.name;
var suffix = file.name.substring(index1 + 1, index2);
//
var timestamp = Date.parse(new Date());
this.QiniuData.key = `upload_pic_${timestamp}.${suffix}`;
},
uploadSuccess(response, index) {
this.$set(this.images, index, `${this.qiniuaddr}/${response.key}`);
this.$set(this.imgValue, index, `${this.qiniuaddr}/${response.key}`);
let str = this.images.join(" ");
this.$emit("moreImages", str);
},
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>
.weekTitle {
text-align: center;
font-size: 18px;
}
.weekImg {
display: inline-block;
margin-right: 15px;
}
.bg-inputWrap {
margin: 10px 0;
}
.avatar-uploader-icon {
height: 180px !important;
line-height: 180px !important;
}
.el-input {
width: 500px;
}
.squareImg {
width: 360px;
height: 360px;
margin: 0 auto;
}
.rectangleImg {
width: 640px;
height: 360px;
margin: 0 auto;
}
</style>
사용 방법
html
<template>
<more-image-upload-vue
v-if="testShow"
:id="dataForm.id == 0 ? 0 : 1"
:imgTotal="4"
:parentImages="dataForm.orgImages"
:parentimgValue="dataForm.orgImages"
@moreImages="moreImagesFun"
>more-image-upload-vue>
template>
js
import moreImageUploadVue from "@/components/image-upload/more-image-upload.vue";
components: {
moreImageUploadVue
},
data(){
return {
testShow:false,
dataForm:{
id:0,
orgImages:""
}
}
},
methods(){
//
moreImagesFun(val) {
this.dataForm.orgImages = val;
},
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
v-bind를 사용하여 CSS에 대한 Vue3 상태애플리케이션을 더 동적으로 만들기 위해 Vue는 동적 상태를 템플릿 내부의 마크업에 연결할 수 있도록 합니다. 예를 들어 특정 기준이 충족되는 경우에만 요소에 클래스를 적용할 수 있습니다. 그러나 구성 요소 상태가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.