11import {
2- computed , defineComponent , ref , toRefs , watch ,
2+ computed , defineComponent , ref , toRefs , watch , onMounted , onUnmounted ,
33} from '@vue/composition-api' ;
44import { isFunction , omit } from 'lodash-es' ;
55import { ImageErrorIcon , ImageIcon } from 'tdesign-icons-vue' ;
66import observe from '../_common/js/utils/observe' ;
77import { useConfig } from '../config-provider/useConfig' ;
8- import { TdImageProps } from './type' ;
98import props from './props' ;
109import { renderTNodeJSX } from '../utils/render-tnode' ;
1110import Space from '../space' ;
@@ -15,7 +14,7 @@ export default defineComponent({
1514 name : 'TImage' ,
1615 components : { Space } ,
1716 props,
18- setup ( props : TdImageProps , { emit } ) {
17+ setup ( props , { emit } ) {
1918 const { onLoad, onError } = props ;
2019
2120 const {
@@ -104,6 +103,17 @@ export default defineComponent({
104103 `${ classPrefix . value } -image--position-${ props . position } ` ,
105104 ] ) ;
106105
106+ const io = ref ( null ) ;
107+
108+ onMounted ( ( ) => {
109+ if ( ! lazy . value || ! imageRef . value ) return ;
110+ io . value = observe ( imageRef . value as HTMLElement , null , handleLoadImage as Function , 0 ) ;
111+ } ) ;
112+
113+ onUnmounted ( ( ) => {
114+ imageRef . value && io && ( io as IntersectionObserver ) . unobserve ( imageRef . value as Element ) ;
115+ } ) ;
116+
107117 return {
108118 imageRef,
109119 imageClasses,
@@ -120,18 +130,11 @@ export default defineComponent({
120130 handleError,
121131 handleLoad,
122132 isLoaded,
133+ io,
123134 rest,
124135 } ;
125136 } ,
126- mounted ( this ) {
127- if ( ! this . lazy || ! this . imageRef ) return ;
128137
129- const io = observe ( this . imageRef as HTMLElement , null , this . handleLoadImage as Function , 0 ) ;
130- this . io = io ;
131- } ,
132- destroyed ( this ) {
133- this . imageRef && this . io && ( this . io as IntersectionObserver ) . unobserve ( this . imageRef as Element ) ;
134- } ,
135138 methods : {
136139 renderPlaceholder ( ) {
137140 const placeholder = renderTNodeJSX ( this , 'placeholder' ) ;
@@ -193,7 +196,6 @@ export default defineComponent({
193196 `${ this . classPrefix } -image__wrapper--shape-${ this . shape } ` ,
194197 this . gallery && `${ this . classPrefix } -image__wrapper--gallery` ,
195198 this . hasMouseEvent && `${ this . classPrefix } -image__wrapper--need-hover` ,
196- this . className ,
197199 ] }
198200 onMouseenter = { this . handleToggleOverlay }
199201 onMouseleave = { this . handleToggleOverlay }
0 commit comments