3
Angular 2 RC4
을 RxJS 5 beta 6
으로 실행하면 내 관찰 가능 항목이 구독자간에 공유되지 않는 이유를 파악하는 데 어려움을 겪고 있습니다. 나는 조심스럽게 지침을 따르려고 노력했다. 내 구성 요소는 다음과 같습니다,하지만 당신은 라이브 at this Plunker각도 2 및 RxJS 5 Observable.share()
템플릿을 실행 볼 수 있습니다
<div> Toggle sub 1:
<button (click)="subOne=!subOne">{{subOne?'One is ON ':'One is OFF'}}</button>
</div>
<div> Toggle sub 2:
<button (click)="subTwo=!subTwo">{{subTwo?'Two is ON ':'Two is OFF'}}</button>
</div>
<ul><li *ngFor="let L of log.slice(-16)">{{L}}</li></ul>
클래스
_subOne = false;
get subOne(){return this._subOne};
set subOne(val){
this._subOne = val;
//if set to true, subscribe
if(val) this.subscriptions.one =
this.source().subscribe(d=>this.print('One sees '+d));
//else, unsubscribe
else this.subscriptions.one.unsubscribe();
}
_subTwo = false;
get subTwo(){return this._subTwo};
set subTwo(val){
this._subTwo = val;
if(val) this.subscriptions.two =
this.source().subscribe(d=>this.print('Two sees '+d));
else this.subscriptions.two.unsubscribe();
}
subscriptions = {'one':null,'two':null};
source(){
return Observable.interval(3000)
.do(()=>this.print("*******EMITTING*******")).share();
}
print(value){this.log.push(value);}
log=[];
출력
.share()
연산자를 사용했기 때문에 구독자가 동일한 관찰 가능 항목을 공유 할 것으로 예상됩니다. 왜 그렇지 않은거야?