【问题标题】:Why subject emits data some times?为什么主题有时会发出数据?
【发布时间】:2021-11-15 17:21:21
【问题描述】:

我们需要你的想法。

组件的构造函数中有一段代码——监听从服务启用isReportMode$报告的模式。

当切换到另一个组件并返回时,isReportMode$ 变量会将值推送两次。

对话框打开两次。虽然在析构函数中我通过takeUntil取消订阅。

组件是:

constructor() 
{
        this.isReportMode$ = this.mapLibrary.reonMap.registryReportService.reportMode$;
        this.isToogleLabelMode$ = this.mapLibrary.reonMap.registryReportService.toggleLabels$;

        this.isReportMode$
            .pipe(
                takeUntil(this.unsubscribe$),
                filter((on) => on),
            )
            .subscribe((on) => {
                console.log(on);
                this.mapLibrary.reonMap.registryReportService.initialize(
                    this.registry,
                    this.registryService.getRegistryBoundaries(this.registry.Id),
                    this.registryService,
                );

                return this.mapLibrary.reonMap.dialogService
                    .open(DialogReportPropertiesComponent, {
                        width: '500px',
                        height: '500px',
                        disableClose: true,
                        position: { top: '10px', right: '10px' },
                        data: {
                            map: this.mapLibrary.reonMap,
                        },
                    })
                    .afterClosed()
                    .subscribe();
            });
    }
}

组件退订:

   ngOnDestroy() {
        this.unsubscribe$.complete();
        this.unsubscribe$.next();
    }

服务是:

export class RegistryReportService {
    public reportModeStatus = false;
    public reportMode$ = new Subject<boolean>();
    
    public toggleMode(): void {
        this.reportModeStatus = !this.reportModeStatus;
        this.reportMode$.next(this.reportModeStatus);
    }

}

所以,当我离开一个组件然后再次进入时,对话框同时打开:console.log(on);。

什么问题?

我还尝试在此处取消订阅对话 stteam:

.afterClosed().pipe(takeUntil(this.unsubscribe$)).subscribe();

没有效果

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    旁白:删除嵌套订阅

    this.isReportMode$.pipe(
      takeUntil(this.unsubscribe$),
      filter(on => on),
    
      tap(on => {
        console.log(on);
        this.mapLibrary.reonMap.registryReportService.initialize(
          this.registry,
          this.registryService.getRegistryBoundaries(this.registry.Id),
          this.registryService
        );
      }),
    
      mergeMap(_ => 
        this.mapLibrary.reonMap.dialogService.open(
          DialogReportPropertiesComponent, 
          {
            width: '500px',
            height: '500px',
            disableClose: true,
            position: { top: '10px', right: '10px' },
            data: {
                map: this.mapLibrary.reonMap,
            },
          }
        ).afterClosed()
    
      )
    ).subscribe();
    

    【讨论】:

      【解决方案2】:

      这是因为unsubscribe$ observable 在发出任何值之前完成,因此之后发出的值将被忽略,因为提供的 observable 已经完成。

      takeUntil:

      发出源 Observable 发出的值,直到通知器 Observable 发出一个值。

      因此,在ngOnDestroy 函数中,您只需在next 和complete 调用之间交换即可解决它。

      【讨论】:

        猜你喜欢
        • 2022-12-07
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 2017-07-28
        • 2020-01-29
        相关资源
        最近更新 更多