[英]RXJS subscribe providing Subject
“訂閱提供主題”有什么用處,這意味着什么:- 1- 為什么我使用訂閱提供主題
import { Subject, from } from 'rxjs';
const subject = new Subject<number>();
subject.subscribe({
next: (v) => console.log(`observerA: ${v}`)
});
subject.subscribe({
next: (v) => console.log(`observerB: ${v}`)
});
const observable = from([1, 2, 3]);
observable.subscribe(subject); // You can subscribe providing a Subject
// Logs:
// observerA: 1
// observerB: 1
// observerA: 2
// observerB: 2
// observerA: 3
// observerB: 3
您使用 Observer 訂閱了 RxJS Observable。 這是用於觀察者的 typescript 接口。
interface Observer<T> {
closed?: boolean;
next: (value: T) => void;
error: (err: any) => void;
complete: () => void;
}
或部分觀察者(僅實現一些回調)或使用 function(與僅具有next
回調的部分觀察者相同。
事實證明,Subject 實現了 Observer 接口,因此它可以用作觀察者。 這是一種多播 observable 的簡單方法(以及多播運營商在一定程度上如何工作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.