master
2022-05-17 d5262ba464cb043f73c8f2af0752011c73f5a663
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
<template>
    <div
        class="cl-image"
        :style="{
            justifyContent: justify,
            height: sty.h
        }"
    >
        <el-image
            :src="urls[0]"
            :fit="fit"
            :lazy="lazy"
            :preview-src-list="urls"
            :style="{
                height: sty.h,
                width: sty.w
            }"
        >
            <template #error>
                <div class="image-slot">
                    <i class="el-icon-picture-outline"></i>
                </div>
            </template>
        </el-image>
    </div>
</template>
 
<script lang="ts">
import { computed, defineComponent } from "vue";
import { isArray, isNumber, isString } from "/@/cool/utils";
 
export default defineComponent({
    name: "cl-image",
 
    props: {
        modelValue: [String, Array],
        src: [String, Array],
        size: {
            type: [Number, Array],
            default: 100
        },
        lazy: {
            type: Boolean,
            default: true
        },
        fit: {
            type: String,
            default: "cover"
        },
        justify: {
            type: String,
            default: "center"
        }
    },
 
    setup(props) {
        const urls = computed(() => {
            const urls: any = props.modelValue || props.src;
 
            if (isArray(urls)) {
                return urls;
            }
 
            if (isString(urls)) {
                return (urls || "").split(",").filter(Boolean);
            }
 
            return [];
        });
 
        const sty = computed(() => {
            const [h, w]: any = isNumber(props.size) ? [props.size, props.size] : props.size;
 
            return {
                h: isNumber(h) ? h + "px" : h,
                w: isNumber(w) ? w + "px" : w
            };
        });
 
        return {
            urls,
            sty
        };
    }
});
</script>
 
<style lang="scss" scoped>
.cl-image {
    display: flex;
    align-items: center;
 
    .el-image {
        display: block;
 
        .image-slot {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            background-color: #f7f7f7;
            border-radius: 3px;
 
            i {
                font-size: 20px;
            }
        }
    }
}
</style>