秦佳浩 3 months ago
commit 587c9a952d

@ -1,77 +1,92 @@
<template>
<image
v-if="!isError && imgPath"
<!-- 根据条件判断显示不同的图片元素 -->
<!-- 如果不存在错误isError为false且图片路径imgPath有值则显示此图片元素 -->
<image v-if="!isError && imgPath"
:src="imgPath"
:style="imgStyle"
:class="classList"
:mode="imgMode"
@error="imgError"
@load="imgLoad"
@tap="handleTap"
/>
<image
v-else
@tap="handleTap" />
<!-- 如果存在错误或者图片路径无值则显示默认图片元素 -->
<image v-else
:src="defaultImgPath"
:style="imgStyle"
:class="classList"
@tap="handleTap"
/>
@tap="handleTap" />
</template>
<script setup>
// 使 defineProps props
const props = defineProps({
//
src: {
type: String,
default: ''
},
// 'scaleToFill'
imgMode: {
type: String,
default: 'scaleToFill'
},
//
classList: {
type: Array,
default: () => {
return []
}
},
//
imgStyle: {
type: Object,
default: () => {
return {}
}
},
// , false, true
// falsetruefalse
defaultImgType: {
type: Boolean,
default: false
}
})
// imgPath util.checkFileUrl props.src
//
const imgPath = computed(() => {
return util.checkFileUrl(props.src)
})
// defaultImgPath defaultImgType
// defaultImgType true
const defaultImgPath = computed(() => {
if (props.defaultImgType) return '/static/images/icon/head04.png'
return '/static/images/icon/def.png'
})
// 使 defineEmits
const emit = defineEmits(['imgError', 'imgLoad', 'handleTap'])
// isError false
const isError = ref(false)
//
// isError true 'imgError'
const imgError = () => {
isError.value = true
emit('imgError')
}
// 'imgLoad' e
const imgLoad = (e) => {
emit('imgLoad', e)
}
// 'handleTap'
const handleTap = () => {
emit('handleTap')
}
</script>
<style scoped>
/* 为 image 标签设置样式,使其宽度和高度都占满父元素的 100% */
image {
width: 100%;
height: 100%;

Loading…
Cancel
Save