第一种:(使用elementUi官网文档中的组件)一般项目中需要点击触发事件才进行查看大图,elementUi组件库中并没有写触发需要执行的代码,这里在触发的方法中加this.$refs.preview.clickHandler()触发查看大图功能
<template>
<!-- 图片预览方法一 -->
<div class="demo-image__preview">
<el-button @click="onPreview">预览</el-button>
<el-image
style="width: 100px; height: 100px"
ref="preview"
:src="url"
:preview-src-list="srcList"
></el-image>
</div>
</template>
<script>
export default {
data() {
return {
url: "https://fuss10.elemecdn.***/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
srcList: [
"https://fuss10.elemecdn.***/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
"https://fuss10.elemecdn.***/8/27/f01c15bb73e1ef3793e64e6b7bb***jpeg.jpeg",
"https://fuss10.elemecdn.***/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg",
],
};
},
***ponents: {},
methods: {
onPreview() {
this.$refs.preview.clickHandler();
},
},
};
</script>
<style></style>
第二种:( 使用el-image-viewer组件,是elementUi自带的但官网文档中可能因不兼容IE没有把它暴露出来,不需要下载包引入即可)
<template>
<div>
<el-button @click="onPreview">预览</el-button>
<el-image
style="width: 100px; height: 100px"
ref="preview"
:src="url"
:preview-src-list="srcList"
></el-image>
<el-image-viewer
v-if="showViewer"
:on-close="closeViewer"
:url-list="srcList"
/>
</div>
</template>
<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
export default {
***ponents: { ElImageViewer },
data() {
return {
showViewer: false,
url: "https://fuss10.elemecdn.***/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
srcList: [
"https://fuss10.elemecdn.***/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
"https://fuss10.elemecdn.***/8/27/f01c15bb73e1ef3793e64e6b7bb***jpeg.jpeg",
"https://fuss10.elemecdn.***/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg",
],
};
},
methods: {
onPreview() {
this.showViewer = true;
},
closeViewer() {
this.showViewer = false;
},
},
};
</script>
<style></style>