// Behavior Subject
// a is an initial value. if there is a subscription// after this, it would get "a" value immediatelylet bSubject = new BehaviorSubject("a");
bSubject.next("b");
bSubject.subscribe(value => {console.log("Subscription got", value); // Subscription got b,// ^ This would not happen// for a generic observable// or generic subject by default});
bSubject.next("c"); // Subscription got cbSubject.next("d"); // Subscription got d
示例2具有常规主题:
// Regular Subject
let subject = new Subject();
subject.next("b");
subject.subscribe(value => {console.log("Subscription got", value); // Subscription won't get// anything at this point});
subject.next("c"); // Subscription got csubject.next("d"); // Subscription got d
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓┃ Observable ┃ BehaviorSubject/Subject ┃┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫┃ Is just a function, no state ┃ Has state. Stores data in memory ┃┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫┃ Code run for each observer ┃ Same code run ┃┃ ┃ only once for all observers ┃┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫┃ Creates only Observable ┃Can create and also listen Observable┃┃ ( data producer alone ) ┃ ( data producer and consumer ) ┃┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫┃ Usage: Simple Observable with only ┃ Usage: ┃┃ one Obeserver. ┃ * Store data and modify frequently ┃┃ ┃ * Multiple observers listen to data ┃┃ ┃ * Proxy between Observable and ┃┃ ┃ Observer ┃┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
let A = new Rx.Subject();let B = new Rx.BehaviorSubject(0);
A.next(1);B.next(1);
A.asObservable().subscribe(n => console.log('A', n));B.asObservable().subscribe(n => console.log('B', n));
A.next(2);B.next(2);
运行此命令ng g s service/message。这将在src\app\service\message.service.ts生成一个服务。提供此服务到应用程序。
将Subject导入MessageService。也添加一个主题。最终代码如下所示:
import { Injectable } from '@angular/core';import { Subject } from 'rxjs/Subject';
@Injectable()export class MessageService {
public message = new Subject<string>();
setMessage(value: string) {this.message.next(value); //it is publishing this value to all the subscribers that have already subscribed to this message}}
// RxJS v6+import { BehaviorSubject } from 'rxjs';
const subject = new BehaviorSubject(123);
//two new subscribers will get initial value => output: 123, 123subject.subscribe(console.log);subject.subscribe(console.log);
//two subscribers will get new value => output: 456, 456subject.next(456);
//new subscriber will get latest value (456) => output: 456subject.subscribe(console.log);
//all three subscribers will get new value => output: 789, 789, 789subject.next(789);
// output: 123, 123, 456, 456, 456, 789, 789, 789
import { ReplaySubject } from 'rxjs';
const behaviorSubject = new BehaviorSubject('hello initial value from BehaviorSubject');
behaviorSubject.subscribe(v => console.log(v));
behaviorSubject.next('hello again from BehaviorSubject');
page = new BehaviorSubject<String|null>(null);actualPage:Observable<string> = new Observable()
this.page.next("hardware")this.actualPage = this.page as Observable<any>;