1
1
import {
2
- computed , defineComponent , ref , toRefs , watch ,
2
+ computed , defineComponent , ref , toRefs , watch , onMounted , onUnmounted ,
3
3
} from '@vue/composition-api' ;
4
4
import { isFunction , omit } from 'lodash-es' ;
5
5
import { ImageErrorIcon , ImageIcon } from 'tdesign-icons-vue' ;
6
6
import observe from '../_common/js/utils/observe' ;
7
7
import { useConfig } from '../config-provider/useConfig' ;
8
- import { TdImageProps } from './type' ;
9
8
import props from './props' ;
10
9
import { renderTNodeJSX } from '../utils/render-tnode' ;
11
10
import Space from '../space' ;
@@ -15,7 +14,7 @@ export default defineComponent({
15
14
name : 'TImage' ,
16
15
components : { Space } ,
17
16
props,
18
- setup ( props : TdImageProps , { emit } ) {
17
+ setup ( props , { emit } ) {
19
18
const { onLoad, onError } = props ;
20
19
21
20
const {
@@ -104,6 +103,17 @@ export default defineComponent({
104
103
`${ classPrefix . value } -image--position-${ props . position } ` ,
105
104
] ) ;
106
105
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
+
107
117
return {
108
118
imageRef,
109
119
imageClasses,
@@ -120,18 +130,11 @@ export default defineComponent({
120
130
handleError,
121
131
handleLoad,
122
132
isLoaded,
133
+ io,
123
134
rest,
124
135
} ;
125
136
} ,
126
- mounted ( this ) {
127
- if ( ! this . lazy || ! this . imageRef ) return ;
128
137
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
- } ,
135
138
methods : {
136
139
renderPlaceholder ( ) {
137
140
const placeholder = renderTNodeJSX ( this , 'placeholder' ) ;
@@ -193,7 +196,6 @@ export default defineComponent({
193
196
`${ this . classPrefix } -image__wrapper--shape-${ this . shape } ` ,
194
197
this . gallery && `${ this . classPrefix } -image__wrapper--gallery` ,
195
198
this . hasMouseEvent && `${ this . classPrefix } -image__wrapper--need-hover` ,
196
- this . className ,
197
199
] }
198
200
onMouseenter = { this . handleToggleOverlay }
199
201
onMouseleave = { this . handleToggleOverlay }
0 commit comments