Operator share()
, Observable, . - . , .share() operator ( .publish().refCount(), . ):
, () Observable. , Observable . , Observable. Observable - , . .publish().refCount().
ionViewDidLoad() {
this.postId = this.navParams.get('postId');
console.log(this.postId);
this.post = this.data.getPostById(this.postId).share();
}
Observable , , (*ngIf), , , .
:
:
{{ (post | async)?.id }}
<div *ngIf="post|async">
{{ (post | async)?.title}}
{{ (post | async)?.content}}
</div>
ts:
post: Observable<Post>;
ionViewDidLoad() {
setTimeout(()=>this.show=true, 5000);
this.postId = this.navParams.get('postId');
console.log(this.postId);
this.post = this.data.getPostById(this.postId).share();
}
share () , , ngIf , Observable, . - :
myPostID
<div></div>
:
.publishReplay(n): n - , Observable. refCount(), reset , .
this.post = this.data.getPostById(this.postId).publishReplay(1).refCount();
*ngIf="post |async as myPost" angular 4 (, , 4.1, ), myPost. . , .share() ts- :
{{ (post | async)?.id }}
<div *ngIf="post|async as myPost">
{{ myPost.title}}
{{ myPost.content}}
</div>
2 publish() refCount():
publish() Observable ConnectableObservable. Observable , connect() ( ).
:
Rx.Observable.prototype.publish([selector])
, , .
refCount() - , ConnectableObservable, connect() . , , .
:
ConnectableObservable.prototype.refCount()
, , .