import { use, isDef, suffixPx } from '../utils'; import Icon from '../icon'; const [sfc, bem] = use('image'); export default sfc({ props: { src: String, fit: String, alt: String, lazyLoad: Boolean, width: [String, Number], height: [String, Number] }, data() { return { loading: true, error: false }; }, watch: { src() { this.loading = true; this.error = false; } }, computed: { style() { const style = {}; if (isDef(this.width)) { style.width = suffixPx(this.width); } if (isDef(this.height)) { style.height = suffixPx(this.height); } return style; } }, created() { const { $Lazyload } = this; if ($Lazyload) { $Lazyload.$on('loaded', this.onLazyLoaded); $Lazyload.$on('error', this.onLazyLoadError); } }, beforeDestroy() { const { $Lazyload } = this; if ($Lazyload) { $Lazyload.$off('loaded', this.onLazyLoaded); $Lazyload.$off('error', this.onLazyLoadError); } }, methods: { onLoad(event) { this.loading = false; this.$emit('load', event); }, onLazyLoaded({ el }) { if (el === this.$refs.image && this.loading) { this.onLoad(); } }, onLazyLoadError({ el }) { if (el === this.$refs.image && !this.error) { this.onError(); } }, onError(event) { this.error = true; this.loading = false; this.$emit('error', event); }, onClick(event) { this.$emit('click', event); }, renderPlaceholder() { if (this.loading) { return (