避免使用嵌套订阅IntersectionObserver。
创始人
2024-12-16 16:30:14
0

使用RxJS的flatMap操作符来将嵌套的订阅操作转换为链式操作。

示例代码:

import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { fromEvent } from 'rxjs';
import { map, flatMap } from 'rxjs/operators';

@Component({
  selector: 'app-my-component',
  template: `
    
Some content
`, }) export class MyComponent implements OnInit { @ViewChild('myDiv') myDiv: ElementRef; ngOnInit(): void { const divIntersection$ = fromEvent(this.myDiv.nativeElement, 'intersection').pipe( map((event: IntersectionObserverEntry) => event.isIntersecting) ); const viewportIntersection$ = fromEvent(window, 'intersection').pipe( map((event: IntersectionObserverEntry) => event.isIntersecting) ); divIntersection$ .pipe( flatMap((divIsIntersecting) => { if (divIsIntersecting) { return viewportIntersection$; } else { return []; } }) ) .subscribe((viewportIsIntersecting) => { // Do something with viewportIsIntersecting }); } }

相关内容

热门资讯

透视关于"xpoke... 透视关于"xpoker怎么作必弊"竟然存在有辅助工具(哔哩哔哩)1、xpoker怎么作必弊免费辅助多...
透视辅助"wepok... 透视辅助"wepoker插件辅助"其实是真的辅助软件(哔哩哔哩)1、下载好wepoker插件辅助脚本...
透视开挂"wepok... 透视开挂"wepoker透视版下载"本来是有辅助方法(哔哩哔哩);在进入wepoker透视版下载软件...
透视有挂"we-po... 透视有挂"we-poker辅助器"确实有辅助器(哔哩哔哩)1、任何we-poker辅助器透视是真的假...
透视了解"pokem... 透视了解"pokemomo辅助软件"总是是真的辅助挂(哔哩哔哩)1、pokemomo辅助软件有没有辅...
透视推荐"wepok... 透视推荐"wepoker辅助器安装包"其实有辅助教程(哔哩哔哩)所有人都在同一条线上,像星星一样排成...
透视揭露"wepok... 透视揭露"wepoker高级辅助"果然是真的辅助技巧(哔哩哔哩)wepoker高级辅助是不是有人用挂...
透视开挂"xpoke... 透视开挂"xpoker辅助工具"好像有辅助挂(哔哩哔哩)1、进入到xpoker辅助工具是否有挂之后,...
透视详细"德普之星有... 透视详细"德普之星有透视辅助吗"果然真的有辅助方法(哔哩哔哩)1、德普之星有透视辅助吗脚本辅助下载、...
透视关于"aapok... 透视关于"aapoker真的假的"竟然真的有辅助神器(哔哩哔哩)1、下载好aapoker真的假的脚本...