在外面写一个隐藏el-image标签,给这个标签加上点击事件
<div>
<el-image
style="display: none"
ref="previewImg"
:src="activeImg"
:preview-src-list="[activeImg]"
></el-image>
<div v-html="caseDetail.article"></div>
</div>
data() {
return {
caseDetail: {},
activeImg: "",
};
},
mounted() {
let vm = this;
vm.$nextTick(() => {
vm.getArticleList().then(() => {
setTimeout(() => {
Array.from(
document.querySelectorAll(".preview-img")
).forEach((img) => {
img.addEventListener(
"click",
vm.previewImgClickHandler,
true
);
});
}, 100);
});
});
},
beforeDestroy() {
let vm = this;
Array.from(document.querySelectorAll(".preview-img")).forEach((img) => {
img.removeEventListener("click", vm.previewImgClickHandler, true);
});
},
methods: {
previewImgClickHandler(evtData) {
let vm = this;
vm.previewImg(evtData.target.getAttribute("src"));
},
previewImg(img) {
let vm = this;
vm.activeImg = img;
vm.$refs.previewImg.clickHandler();
},
getArticleList() {
vm.caseDetail = res.data;
vm.caseDetail.article = vm.caseDetail.article.replace(
/(\< img [^\<\>]*src=\"[^\"]+\")([^\<\>]*\/\>)/g,
(a, p1, p2, c, d) => {
return `${p1} class="preview-img" ${p2}`;
}
);
},
},
|