Skip to content

Commit

Permalink
fix(runtime-web): this.createIntersectionObserver 指定 selector 为自定义组件的…
Browse files Browse the repository at this point in the history
…最外层节点时导致触发异常 (eleme#104)
  • Loading branch information
hwaphon authored Aug 31, 2023
1 parent 2ef37e0 commit c22c2a2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/runtime-web/src/runtime/public/utils/instanceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IntersectionObserverOptions } from '../../../api/ui/element-query/inter

export function mountIntersectionObserver(
options: IntersectionObserverOptions,
root: Element | Text | null
root: HTMLElement | null
) {
const intersectionObserver = my.createIntersectionObserver(options)
// 保存一份引用,且固定 this 绑定
Expand All @@ -12,8 +12,14 @@ export function mountIntersectionObserver(
intersectionObserver.observe = (targetSelector, callback) => {
try {
let element = null
if (root && root.parentNode)
element = root.parentNode.querySelector(targetSelector)
if (root) {
// 查看要遍历的组件是不是自定义节点最外层的节点
if (typeof root.matches === 'function' && root.matches(targetSelector))
element = root
else if (root.parentNode)
element = root.parentNode.querySelector(targetSelector)
}

// 在这种场景下,查找元素要从当前自定义节点下查找,不能从全局角度查找
if (element) {
return observe(element, callback)
Expand Down

0 comments on commit c22c2a2

Please sign in to comment.