Angular RxJS ShareReplay和PublishReplay
我理解ShareReplay和PublishReplay之间的区别,但在下面的示例中,我可以使用一些说明来说明为什么在前一个PublishReplay之后不久的PublishReplay不会发出任何事件,但ShareReplay会发出任何事件 我有一个Angular服务,它向几个组件提供一些API数据。我使用publishReplay将数据多播到组件,而无需多次重新读取数据,一旦所有组件取消订阅,数据就会被清除Angular RxJS ShareReplay和PublishReplay,angular,rxjs6,Angular,Rxjs6,我理解ShareReplay和PublishReplay之间的区别,但在下面的示例中,我可以使用一些说明来说明为什么在前一个PublishReplay之后不久的PublishReplay不会发出任何事件,但ShareReplay会发出任何事件 我有一个Angular服务,它向几个组件提供一些API数据。我使用publishReplay将数据多播到组件,而无需多次重新读取数据,一旦所有组件取消订阅,数据就会被清除 @Injectable() export class APIService {
@Injectable()
export class APIService {
data = this.http.get(url).pipe(
publishReplay(1)
);
}
组件在收到数据事件后构建FormControls,并且仍然使用html中的原始数据,因此点击
@Component()
export class SomeComponent {
data = this.apiService.data.pipe(
tap((data) => buildControls(data))
);
}
在一些情况下,我需要在formControls构建之后从中创建一些可观察对象。我需要多播它,否则formControls将在每个后续订阅上重建,并且不能正确绑定
@Component()
export class SomeComponent {
data = this.apiService.data.pipe(
tap((data) => buildControls(data)),
shareReplay(1) // PublishReplay does not work here
);
someObservable = this.data.pipe(
switchMap(() => buildLinkedControls())
);
}
在上面的组件示例中,为什么PublishReplay不触发任何事件,而shareReplay却触发任何事件。在这里使用shareReplay是否有任何危险,因为缓冲区将继续存在
为了完整起见,下面是一个简单的html示例
<ng-container *ngIf="(data | async)?.properties as props">
<input *ngIf="props?.someInput" [formControl]="formGroup.get('someInput')">
...
<div>{{ someObservable | async }}</div?
</ng-container>
...
{{someObservable | async}}你说在这里使用shareReplay有什么危险吗?你是说内存泄漏吗?顺便说一句,shareReplay
是您所描述的正确操作符。您所说的在这里使用shareReplay有任何危险是什么意思?你是说内存泄漏吗?顺便说一下,shareReplay
是您描述的正确操作符。