diff --git a/src/image/index.js b/src/image/index.js index 822eabcda..46e129166 100644 --- a/src/image/index.js +++ b/src/image/index.js @@ -1,4 +1,5 @@ -import { createNamespace, isDef, addUnit, inBrowser } from '../utils'; +import { ref, watch, computed } from 'vue'; +import { createNamespace, isDef, addUnit } from '../utils'; import Icon from '../icon'; const [createComponent, bem] = createNamespace('image'); @@ -31,156 +32,138 @@ export default createComponent({ }, }, - emits: ['load', 'error', 'click'], + emits: ['load', 'error'], - data() { - return { - loading: true, - error: false, - }; - }, + setup(props, { emit, slots }) { + const error = ref(false); + const loading = ref(true); + const imageRef = ref(null); - watch: { - src() { - this.loading = true; - this.error = false; - }, - }, - - computed: { - style() { + const style = computed(() => { const style = {}; - if (isDef(this.width)) { - style.width = addUnit(this.width); + if (isDef(props.width)) { + style.width = addUnit(props.width); } - if (isDef(this.height)) { - style.height = addUnit(this.height); + if (isDef(props.height)) { + style.height = addUnit(props.height); } - if (isDef(this.radius)) { + if (isDef(props.radius)) { style.overflow = 'hidden'; - style.borderRadius = addUnit(this.radius); + style.borderRadius = addUnit(props.radius); } return style; - }, - }, + }); - created() { - const { $Lazyload } = this; - - if ($Lazyload && inBrowser) { - $Lazyload.$on('loaded', this.onLazyLoaded); - $Lazyload.$on('error', this.onLazyLoadError); - } - }, - - beforeUnmount() { - 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(); + watch( + computed(() => props.src), + () => { + error.value = false; + loading.value = true; } - }, + ); - onLazyLoadError({ el }) { - if (el === this.$refs.image && !this.error) { - this.onError(); - } - }, + const onLoad = (event) => { + loading.value = false; + emit('load', event); + }; - onError(event) { - this.error = true; - this.loading = false; - this.$emit('error', event); - }, + const onError = (event) => { + error.value = true; + loading.value = false; + emit('error', event); + }; - onClick(event) { - this.$emit('click', event); - }, - - genPlaceholder() { - if (this.loading && this.showLoading) { - return ( -