Rxjs BehaviorSuject 和 Observable 的区别
https://stackoverflow.com/questions/39494058/behaviorsubject-vs-observable
BehaviorSubject is a type of subject, a subject is a special type of observable so you can subscribe to messages like any other observable.
BehaviorSubhect 是一種特殊的 Observable.
The unique features of BehaviorSubject are:
(1) It needs an initial value as it must always return a value on subscription even if it hasn’t received a next().
完整例子:
import { Component, OnInit } from '@angular/core'; import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';@Component({selector: 'app-behavior-subject',templateUrl: './behavior-subject.component.html' }) export class BehaviorSubjectComponent implements OnInit {ngOnInit(): void {const subject = new BehaviorSubject(123);// two new subscribers will get initial value => output: 123, 123subject.subscribe((data) => console.log('sub 1: ', data));subject.subscribe((data) => console.log('sub 2: ', data));// two subscribers will get new value => output: 456, 456console.log('subject emits 456!');subject.next(456);// new subscriber will get latest value (456) => output: 456subject.subscribe((data) => console.log('sub 3: ', data));// all three subscribers will get new value => output: 789, 789, 789console.log('subject emits 789!');subject.next(789);} }立即打印初始值: 123 123
一旦 BehaviorSubject 被調用 subscribe 方法進行訂閱,訂閱函數會立即收到最新的數據。
Upon subscription, it returns the last value of the subject. A regular observable only triggers when it receives an onnext.
https://stackoverflow.com/questions/39494058/behaviorsubject-vs-observable
One very very important difference. Since Observable is just a function, it does not have any state, so for every new Observer, it executes the observable create code again and again. This results in:
The code is run for each observer . If its a HTTP call, it gets called for each observer
Observer 只是函數。
BehaviorSubject (or Subject ) stores observer details, runs the code only once and gives the result to all observers .
Observables
-
They are cold: Code gets executed when they have at least a single observer.
-
Creates copy of data: Observable creates copy of data for each observer.
-
Uni-directional: Observer can not assign value to observable(origin/master).
Subject
-
They are hot: code gets executed and value gets broadcast even if there is no observer.
-
Shares data: Same data get shared between all observers.
-
bi-directional: Observer can assign value to observable(origin/master).
-
If are using using subject then you miss all the values that are broadcast before creation of observer. So here comes Replay Subject
ReplaySubject
-
They are hot: code gets executed and value get broadcast even if there is no observer.
-
Shares data: Same data get shared between all observers.
-
bi-directional: Observer can assign value to observable(origin/master). plus
-
Replay the message stream: No matter when you subscribe the replay subject you will receive all the broadcasted messages.
-
In subject and replay subject you can not set the initial value to observable. So here comes Behavioral Subject
BehaviorSubject
-
They are hot: code gets executed and value get broadcast even if there is no observer.
-
Shares data: Same data get shared between all observers.
-
bi-directional: Observer can assign value to observable(origin/master). plus
-
Replay the message stream: No matter when you subscribe the replay subject you will receive all the broadcasted messages.
-
You can set initial value: You can initialize the observable with default value.
The Observable object represents the object that sends notifications (the provider); the Observer object represents the class that receives them (the observer).
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的Rxjs BehaviorSuject 和 Observable 的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是 Linux Foundation
- 下一篇: 降价9万后雪铁龙C6销量暴涨!总经理:价