日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Rxjs BehaviorSuject 和 Observable 的区别

發(fā)布時(shí)間:2023/12/19 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Rxjs BehaviorSuject 和 Observable 的区别 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 被調(diào)用 subscribe 方法進(jìn)行訂閱,訂閱函數(shù)會(huì)立即收到最新的數(shù)據(jù)。

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 只是函數(shù)。

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的原創(chuàng)文章,盡在:“汪子熙”:

總結(jié)

以上是生活随笔為你收集整理的Rxjs BehaviorSuject 和 Observable 的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。