秦佳浩 3 months ago
commit 587c9a952d

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

Loading…
Cancel
Save