【发布时间】: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();
没有效果
【问题讨论】: