[RxJS] Get current value out of Subject (BehaviorSubject)

When you want to get the current value of a subject, you need to switch BehaviorSubject, it always emit the latest value or throw error.

Then you can call ‘getValue()’:

var subject = new Rx.BehaviorSubject(56);

console.log('Value is: ' + subject.getValue());

// => Value is: 56

subject.onNext(42);

console.log('Value is: ' + subject.getValue());

// => Value is: 42

subject.onCompleted();

subject.onNext(100);

console.log('Value is frozen: ' + subject.getValue());

// => Value is frozen: 42

subject.dispose();

try {
    subject.getValue();
} catch (e) {
    console.log(e.message);
}

// => Object has been disposed

 

    原文作者:Zhentiw
    原文地址: https://www.cnblogs.com/Answer1215/p/9321308.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞