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

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

问题描述

我发现 rxJS 文档将它们定义为

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

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?

谢谢!

推荐答案

理解它的最简单方法是将 Subject 视为同时是 producer消费者.这就像一个开放的频道,有人可以在一端发送消息,而任何订阅者都会在另一端收到消息.

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);
  }
}

注意 messages getter 将 Subject 作为 Observable 返回.这不是必需的.Subject 已经是一个 observable,任何人都可以直接订阅 Subject.但我认为 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 实际上是一个Subject.当我们订阅 EventEmitter 时,我们订阅了一个 Subject,当我们在 EventEmitteremit 时,我们正在通过 Subject 为所有订阅者发送消息.

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.

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

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