作者 RuoYi

预览组件支持多图显示

1 <template> 1 <template>
2 - <el-image :src="`${realSrc}`" fit="cover" :style="`width:${realWidth};height:${realHeight};`" :preview-src-list="[`${realSrc}`]">  
3 - <div slot="error" class="image-slot">  
4 - <i class="el-icon-picture-outline"></i>  
5 - </div>  
6 - </el-image> 2 + <el-image
  3 + :src="`${realSrc}`"
  4 + fit="cover"
  5 + :style="`width:${realWidth};height:${realHeight};`"
  6 + :preview-src-list="realSrcList"
  7 + >
  8 + <div slot="error" class="image-slot">
  9 + <i class="el-icon-picture-outline"></i>
  10 + </div>
  11 + </el-image>
7 </template> 12 </template>
8 13
9 <script> 14 <script>
10 -import { isExternal } from '@/utils/validate' 15 +import { isExternal } from "@/utils/validate";
11 16
12 export default { 17 export default {
13 - name: 'ImagePreview',  
14 - props: {  
15 - src: {  
16 - type: String,  
17 - required: true  
18 - },  
19 - width: {  
20 - type: [Number, String],  
21 - default: ''  
22 - },  
23 - height: {  
24 - type: [Number, String],  
25 - default: ''  
26 - } 18 + name: "ImagePreview",
  19 + props: {
  20 + src: {
  21 + type: String,
  22 + required: true
  23 + },
  24 + width: {
  25 + type: [Number, String],
  26 + default: ""
27 }, 27 },
28 - computed: {  
29 - realSrc() {  
30 - if (isExternal(this.src)) {  
31 - return this.src  
32 - }  
33 - return process.env.VUE_APP_BASE_API + this.src  
34 - },  
35 - realWidth() {  
36 - return typeof this.width == 'string' ? this.width : `${this.width}px`  
37 - },  
38 - realHeight() {  
39 - return typeof this.height == 'string' ? this.height : `${this.height}px` 28 + height: {
  29 + type: [Number, String],
  30 + default: ""
  31 + }
  32 + },
  33 + computed: {
  34 + realSrc() {
  35 + let real_src = this.src.split(",")[0];
  36 + if (isExternal(real_src)) {
  37 + return real_src;
  38 + }
  39 + return process.env.VUE_APP_BASE_API + real_src;
  40 + },
  41 + realSrcList() {
  42 + let real_src_list = this.src.split(",");
  43 + let srcList = [];
  44 + real_src_list.forEach(item => {
  45 + if (isExternal(item)) {
  46 + return srcList.push(item);
40 } 47 }
  48 + return srcList.push(process.env.VUE_APP_BASE_API + item);
  49 + });
  50 + return srcList;
  51 + },
  52 + realWidth() {
  53 + return typeof this.width == "string" ? this.width : `${this.width}px`;
  54 + },
  55 + realHeight() {
  56 + return typeof this.height == "string" ? this.height : `${this.height}px`;
41 } 57 }
42 -} 58 + },
  59 +};
43 </script> 60 </script>
44 61
45 <style lang="scss" scoped> 62 <style lang="scss" scoped>
46 .el-image { 63 .el-image {
47 - border-radius: 5px;  
48 - background-color: #ebeef5;  
49 - box-shadow: 0 0 5px 1px #ccc;  
50 - ::v-deep .el-image__inner {  
51 - transition: all 0.3s;  
52 - cursor: pointer;  
53 - &:hover {  
54 - transform: scale(1.2);  
55 - }  
56 - }  
57 - ::v-deep .image-slot {  
58 - display: flex;  
59 - justify-content: center;  
60 - align-items: center;  
61 - width: 100%;  
62 - height: 100%;  
63 - color: #909399;  
64 - font-size: 30px; 64 + border-radius: 5px;
  65 + background-color: #ebeef5;
  66 + box-shadow: 0 0 5px 1px #ccc;
  67 + ::v-deep .el-image__inner {
  68 + transition: all 0.3s;
  69 + cursor: pointer;
  70 + &:hover {
  71 + transform: scale(1.2);
65 } 72 }
  73 + }
  74 + ::v-deep .image-slot {
  75 + display: flex;
  76 + justify-content: center;
  77 + align-items: center;
  78 + width: 100%;
  79 + height: 100%;
  80 + color: #909399;
  81 + font-size: 30px;
  82 + }
66 } 83 }
67 </style> 84 </style>