秦佳浩 3 months ago
commit 587c9a952d

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

Loading…
Cancel
Save