2

我有一个侧边栏,当您对其进行更改时,我需要订阅这些更改并对它们做出反应,因此我们使用的是主题。

问题是我们不知道 Subject 的最佳架构,它应该是发出的单个字符串还是对象?

如果我们将所有更改的对象作为键:值对发出,我们如何在其他地方仅订阅其中一个更改而不是对整个对象做出反应?

我想避免每次更改都使用主题发射器污染我的代码,或者这是“最佳实践”

当前实施的示例

发射器

/**
 * A function that emits an RXJS event to any observers subscribed to this subjet
 * when the user changes the calendar in the sidebar.
 * @return {Observable} An Observable that, whenever subscribed, will execute the
 * specified function.
 * @static false
 */
public emitCalendar = (calendar: any): void => {
    this.selectedCalendar = calendar;
    this.onChangeLeftPane$.next({ calendar: calendar });
};

那么订阅的最佳方式是什么

onChangeLeftPane$.calendar
4

1 回答 1

0

设置一个单独的 Observable 来监听filter操作员的变化

    this.onChangeCalendar=this.onChangeLeftPane$.asObservable()
.pipe(filter(obj=>Objects.keys(obj).includes('calendar'))
this.onChangeLeftPane$.next({ calendar: calendar });
于 2019-05-06T14:06:57.990 回答