angular - 角度的动态组件订阅
问题描述
我正在通过ngFor循环渲染一些动态组件,我希望这些组件能够相互通信。我这样做的第一个想法是从辅助状态服务(myComunicationService )订阅一些主题更改:
HTML
<div *ngFor="let entry of dataList">
<custom-component [entry]="entry"/>
</div>
自定义组件 ts
export class CustomComponent implements OnInit {
@Input() entry: Entry;
private subscriptions: Subscription = new Subscription();
constructor(private myComunicationService: MyComunicationService) {}
public ngOnDestroy(): void {
this.subscriptions.unsubsribe();
}
public ngOnInit(): void {
this.subscriptions.add(
this.myComunicationService.dataChanged$.subscribe(
(dataChanged) => {
if (isForThisComponentInstance(dataChanged)) {
// do stuff for this component instance only
}
}
);
);
}
// private isForThisComponentInstance = (dataChanged) => boolean
// this method checks some unique property that matches with the instance component
}
所以基本上我所有的组件都在监听同一个事件,它们都在捕捉那个事件,并且if { }语句是某种过滤器,它实际上允许组件改变/做事情。(对这种方法不太满意)
如何改进这一点而不依赖于 if 语句?
解决方案
而不是使用
this.myComunicationService.dataChanged$
直接在您的组件中,您可以要求服务为您做一些额外的工作。
说this.myComunicationService.getChangesFor(this)
然后为您服务
getChangesFor(compy: CustomComponent): Observable<any>{
return this.dataChanged$.pipe(
filter(data => data.id === compy.id)
);
}
这实际上是同一件事,但是从可能不属于它的组件中删除了逻辑。这样如果 10 个不同的组件都调用这个服务,它们就不会都单独实现这个逻辑。它还使您可以自由更改通信服务处理问题的方式。
例如:
- 您可以为每个组件创建一个新的主题/重播主题(例如,将它们推入一个数组)。然后,不要过滤,只针对您知道(或可能)需要数据的组件。
- 您可以定义与组件无关的命名法和/或接口,它决定哪些类型的数据适合过滤。
然而,在某些时候,哪些数据去哪里的逻辑取决于你。
推荐阅读
- node.js - async/await 是否以不同的方式处理未捕获的拒绝错误?
- python - 如何在 Google Playground 协作中使用 EndlessGPT2.py
- random-forest - 避免过度拟合随机森林
- javascript - 附加后隐藏和显示按钮不起作用
- sql - SQL Server 使 varbinary(8000) 成为主键
- python-3.x - 为什么录制文件在python中是空的
- node.js - 聚合匹配的MongoDB查找返回一个空数组
- python - Groupby 值如何计算 pandas 数据帧?
- c# - 使用 Pushsharp C# 的 IOS MDM 推送通知问题
- c# - Elasticsearch - 嵌套 - 映射 - 多目标别名或仅指定 1 个多字段子字段以用于每个文档