什么是RxJS主题和使用它们的好处? [英] What are RxJS Subject's and the benifits of using them?

查看:223
本文介绍了什么是RxJS主题和使用它们的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 rxJS docs 将它们定义为


什么是主题? RxJS主题是一种特殊类型的Observable,允许将值多播到许多观察者。虽然普通的Observable是单播的(每个订阅的Observer都拥有Observable的独立执行),但是Subjects是多播的。

What is a Subject? An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.

它继续举例,但我正在寻找一个基本的ELI5解释。根据我的理解,它有助于处理和定义序列中的项目。那是对的吗?

and it goes on to give examples but I'm looking for a basic ELI5 explanation. From my understanding is it helps handle and define items in a sequence. Is that correct?

我认为,对于我和其他人来说,看看一个简单的功能,无论是否定义 rxJS主题,都能帮助我理解为什么它很重要?

I think it would be most helpful to me and others to see a simple function with and without defining an rxJS Subject to understand why it's important?

谢谢!

推荐答案

理解它的最简单方法是将主题视为生产者消费者。这就像一个开放的频道,有人可以在一端发送消息,任何订阅者都会在另一端接收消息。

The easiest way to understand it is to think of a Subject as both a producer and a consumer. It's like an open channel where someone can send a message on one end, and any subscribers will receive it on the other end.

                                  +---------------
Sender                            | =>  =>  =>  =>  Subscriber
           -----------------------+   +----------- 
Message =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  Subscriber
           -----------------------+   +-----------
                                  | =>  =>  =>  =>  Subscriber
                                  +---------------

在代码术语中说你有一个主题服务

In code terms say you have a service with a subject

class MessageService {
  private _messages = new Subject<Message>();

  get messages: Observable<Message> {
    return this._messages.asObservable();
  }

  sendMessage(message: Message) {
    this._messages.next(message);
  }
}

注意消息 getter将Subject作为Observable返回。这不是必需主题已经是一个可观察的,任何人都可以直接订阅主题。但我认为 asObservable 模式用作限制用户可以使用它的方式,即用户只能用它来订阅,而不是发出。我们保存 sendMessage 方法的发射。

Note the messages getter returning the Subject as an Observable. This is not required. The Subject is already an observable, and anybody could subscribe directly to the Subject. But I think the asObservable pattern is used as a way to limit what users can do with it, i.e. so users only use it to subscribe to, and not emit to. We save the emitting for the sendMessage method.

现在有了这个服务,我们可以将它注入不同的组件,这可以是两个(或更多)任意组件进行通信(或只是接收任意事件通知)的方式。

Now with this service in place, we can inject it into different components, and this can be a way for two (or more) arbitrary components to communicate (or just receive arbitrary event notifications).

class ComponentOne {
  constructor(private messages: MessageService) {}

  onClick() {
    this.messages.sendMessage(new Message(..));
  }
}

class ComponentTwo {
  constructor(private messages: MessageService) {}

  ngOnInit() {
    this.messages.messages.subscribe((message: Message) => {
      this.message = message;
    });
  }
}

Angular自己的 EventEmitter 实际上是主题。当我们订阅 EventEmitter 时,我们订阅 Subject ,当我们发出时 EventEmitter 上的,我们正在通过主题为所有订阅者发送消息。

Angular's own EventEmitter is actually a Subject. When we subscribe to the EventEmitter, we are subscribing to a Subject, and when we emit on the EventEmitter, we are sending a message through the Subject for all subscribers.

  • Subject vs BehaviorSubject vs ReplaySubject in Angular

这篇关于什么是RxJS主题和使用它们的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆