Skip to content

[Checkbox] lazyLoad 观察器清理会报错或泄漏 #6853

Closed

Description

@zhangpaopao0609

关联

问题描述

useCheckboxLazyLoad 的显示状态和观察器清理依赖当前 lazyLoad 值,并假定 observe() 一定返回 IntersectionObserver,导致两个可复现问题。

相关源码:

watch([lazyLoad, labelRef], handleLazyLoad);

onBeforeUnmount(() => {
  if (!lazyLoad.value) return;
  ioObserver.value.unobserve(labelRef.value);
});

场景一:环境没有 IntersectionObserver

公共 observe() 工具会同步执行回调并返回 null。组件可以显示,但卸载时执行 null.unobserve(...),Vue 报出 TypeError 生命周期错误。

这会影响未提供 IntersectionObserver 的 jsdom、SSR 衔接环境以及部分旧运行环境。

场景二:运行时关闭 lazyLoad

Checkbox 在尚未相交、内容仍隐藏时从 lazyLoad=true 改为 false:

  • handleLazyLoad 直接 return,内容仍然隐藏;
  • 卸载时又因当前值为 false 直接 return,已经创建的 observer 不会解绑。

预期结果

  • 清理逻辑对空 observer 安全;
  • 是否清理取决于 observer 是否存在,而不是卸载时的 lazyLoad 值;
  • 关闭 lazyLoad 后立即显示内容并清理已有 observer;
  • prop 多次切换时不遗留观察器。

补充说明

该问题是在基于最新 develop(9e47c828a)完全重写 Checkbox 单元测试时发现。测试 PR 不修改源码,会按当前真实行为保留特征测试;源码修复时应同步更新断言。

Activity

  1. github-actions commented on Aug 1, 2026

    @github-actions
    Contributor

    👋 @zhangpaopao0609,感谢给 TDesign 提出了 issue。
    请根据 issue 模版确保背景信息的完善,我们将调查并尽快回复你。

  2. zhangpaopao0609 commented on Aug 1, 2026

    @zhangpaopao0609
    CollaboratorAuthor

    关联测试 PR:#6855。该 PR 不修改源码,按当前真实行为保留特征测试;源码修复时请同步更新对应断言。

  3. kyui-azusa commented on Aug 3, 2026

    @kyui-azusa
    Contributor

    I'd like to work on this issue. I'll make observer cleanup depend on the observer instance, reveal content when lazyLoad is disabled, and cover both runtime toggling and environments without IntersectionObserver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions