Skip to content

Commit ca39f68

Browse files
author
zhuqingan.3
committed
fix: 修复异常关闭Image可能导致的api报错
1 parent 785d050 commit ca39f68

File tree

1 file changed

+5
-2
lines changed
  • packages/taro-components/src/components/image

1 file changed

+5
-2
lines changed

packages/taro-components/src/components/image/image.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export class Image implements ComponentInterface {
4141
private imgRef: HTMLImageElement
4242

4343
componentDidLoad () {
44-
if (!this.lazyLoad) return
44+
if (!this.lazyLoad || !this.imgRef) return
4545

4646
const lazyImg = new IntersectionObserver(entries => {
4747
// 异步 api 关系
4848
if (entries[entries.length - 1].isIntersecting) {
49-
lazyImg.unobserve(this.imgRef)
49+
this.imgRef && lazyImg.unobserve(this.imgRef)
5050
this.didLoad = true
5151
}
5252
}, {
@@ -57,6 +57,9 @@ export class Image implements ComponentInterface {
5757
}
5858

5959
imageOnLoad () {
60+
// 防止组件已卸载或 img 已被移除时 ref 为 null(如列表滚动、src 清空、lazyLoad 时序等)
61+
if (!this.imgRef) return
62+
6063
const {
6164
width,
6265
height,

0 commit comments

Comments
 (0)