前端头像加载失败则使用默认头像

This commit is contained in:
John Smith 2020-10-11 13:14:10 +08:00
parent 0e060e61fa
commit 0e06c4ba24

View File

@ -1,16 +1,35 @@
<template>
<yt-img-shadow class="no-transition" :height="height" :width="width" style="background-color: transparent;" loaded>
<img id="img" class="style-scope yt-img-shadow" alt="" :height="height" :width="width" :src="imgUrl">
<img id="img" class="style-scope yt-img-shadow" alt="" :height="height" :width="width" :src="showImgUrl" @error="onLoadError">
</yt-img-shadow>
</template>
<script>
import {DEFAULT_AVATAR_URL} from '@/api/chat/avatar'
export default {
name: 'ImgShadow',
props: {
imgUrl: String,
height: String,
width: String
},
data() {
return {
showImgUrl: this.imgUrl
}
},
watch: {
imgUrl(val) {
this.showImgUrl = val
}
},
methods: {
onLoadError() {
if (this.showImgUrl !== DEFAULT_AVATAR_URL) {
this.showImgUrl = DEFAULT_AVATAR_URL
}
}
}
}
</script>