避免使用嵌套订阅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 }); } }

相关内容

热门资讯

此事迅速冲上热搜!德州真人透视... 此事迅速冲上热搜!德州真人透视脚本,德普之星透视辅助插件,原来确实有挂(真的有挂)1、德普之星透视辅...
出乎意料的是!wepoker游... 出乎意料的是!wepoker游戏安装教程,德普之星透视软件免费入口官网,总是真的有挂(果真有挂)1、...
经调查!wpk模拟器是什么,德... 经调查!wpk模拟器是什么,德普之星透视,其实确实有挂(有挂功能)1、金币登录送、破产送、升级送、活...
突发!sohoo poker辅... 突发!sohoo poker辅助器,德扑之星透视,切实是有挂(有挂方略)1、德扑之星透视辅助器安装包...
截至目前!wepoker私人局... 截至目前!wepoker私人局辅助,德普辅助软件,原来真的是有挂(有挂技巧)1、德普辅助软件公共底牌...
近日!wepoker怎么看牌型... 近日!wepoker怎么看牌型,德普软件,总是是有挂(有挂秘籍)1、德普软件模拟器是什么优化,德普软...
据权威媒体报道!wpk透视辅助... 据权威媒体报道!wpk透视辅助靠谱吗,德普之星透视免费,总是真的有挂(有挂技巧)1、德普之星透视免费...
目前!pokemmo手机版透视... 目前!pokemmo手机版透视脚本,德扑之星透视软件免费入口官网,本来真的是有挂(有挂助手)1、德扑...
据通报!wpk刷入池率脚本,德... 据通报!wpk刷入池率脚本,德扑之星私人局辅助器,竟然是真的有挂(有挂教程)1.德扑之星私人局辅助器...
不少玩家反映!wepoker游... 不少玩家反映!wepoker游戏下载,德扑之星有辅助软件吗,切实是真的有挂(有挂总结)1.德扑之星有...