简体   繁体   English

Observable的Observable数组-如何在RxJS中实现?

[英]Observable Array of Observables - How to implement in RxJS?

I'm just learning RxJS for the first time, so I apologize if this question is based on false assumptions. 我是第一次学习RxJS,因此,如果这个问题是基于错误的假设,我深表歉意。

Say, for example, that I have a FooService in my Angular app that returns an Array<Foo> . 例如,假设我的Angular应用程序中有一个FooService,它返回Array<Foo> Elements may be added and removed from the list arbitrarily. 元素可以从列表中任意添加和删除。 Therefore, I wrap my Array of Foos in an RxJS observable; 因此,我将Foos数组包装在可观察的RxJS中。 the FooService now returns a BehavioralSubject<Array<Foo>> . FooService现在返回BehavioralSubject<Array<Foo>> Now I can use Angular's AsyncPipe to automatically rerender the necessary components every time the BehavioralSubject is updated. 现在,我可以在每次BehavioralSubject更新时使用Angular的AsyncPipe自动重新呈现必要的组件。 So far, this is trivial RxJS 101. 到目前为止,这是微不足道的RxJS 101。

But in my app, not only can the Array of Foos receive updates, but each individual Foo can receive updates without affecting the Array. 但是在我的应用程序中,Foos Array不仅可以接收更新,而且每个Foo都可以接收更新而不会影响Array。 How do I architect this? 我该如何设计? Do I wrap each individual Foo in an Observable, making my FooService return a BehavioralSubject<Array<BehavioralSubject<Foo>>> ? 我是否将每个单独的Foo包装在Observable中,以使我的FooService返回BehavioralSubject<Array<BehavioralSubject<Foo>>> This seems messy. 这看起来很混乱。 Do I continue to return a BehavioralSubject<Array<Foo>> , and rerender the entire Array component every time a Foo updates? 我是否会继续返回BehavioralSubject<Array<Foo>> ,并在每次Foo更新时重新渲染整个Array组件? This tightly binds Foo updates to Array updates, preventing me from reusing my Foo component in other parts of the app. 这将Foo更新与Array更新紧密地绑定在一起,从而防止我在应用程序的其他部分重用Foo组件。

What's the best way to do this in RxJS? 在RxJS中执行此操作的最佳方法是什么? How do I (actually) implement a (conceptual) Observable Array of Observables? 我如何(实际上)实现(概念上)可观察对象数组?

But in my app, not only can the Array of Foos receive updates, but each individual Foo can receive updates without affecting the Array. 但是在我的应用程序中,Foos Array不仅可以接收更新,而且每个Foo都可以接收更新而不会影响Array。

This doesn't make sense to me. 这对我来说没有意义。 If you have an array that holds foos, and one of the foos gets changed, the array inherently changes. 如果您有一个包含foos的数组,并且其中一个foos被更改,则该数组会固有地更改。

If you are using the ngFor directive, change propagation in your template will be optimised so you won't have to worry about heavy DOM lifting in most use-cases: https://angular.io/api/common/NgForOf#change-propagation 如果您使用的是ngFor指令,则将优化模板中的更改传播,因此您不必担心在大多数用例中会出现大量的DOM: https : //angular.io/api/common/NgForOf#change-传播

In case you need more control, check this out: https://angular.io/guide/template-syntax#ngfor-with-trackby 如果您需要更多控制权,请查看以下网址https : //angular.io/guide/template-syntax#ngfor-with-trackby

Now, in your service, you can do this: 现在,在您的服务中,您可以执行以下操作:

private foos = new BehaviorSubject<Foo[]>([]);

Then create a function that returns an Observable to be used in your template: 然后创建一个返回要在模板中使用的Observable的函数:

get() { 
    return this.foos.asObservable(); 
}

Then you add logic to change foos the way you like, for example, add a foo: 然后添加逻辑改变foos你喜欢的方式,例如,添加一个Foo:

add(foo: Foo) {
    let arr = this.foos.getValue();
    arr.push(foo);
    this.foos.next(arr);
}

Change a foo: 更改一个foo:

change(index: number, foo: Foo) {
    let arr = this.foos.getValue();
    arr[index] = foo;
    this.foos.next(arr);
}

Then, in your component, use fooService.get() | async 然后,在您的组件中,使用fooService.get() | async fooService.get() | async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM