繁体   English   中英

TypeScript:将新的 Observable 分配给初始化的 Observable 变量

[英]TypeScript: Assign new Observable to initialized Observable variable

只是为了检查这个是否有隐藏的渔获物。

想象一个带有@Input() setter 函数的组件的情况,其中私有变量与分配给另一个变量的 NgRx 选择器一起设置:

private _inputValue: SomeDto;
asyncProperty$: Observable<boolean>;

@Input() set someValue(inputValue: SomeDto) {
  this._inputValue = inputValue
  this.asyncProperty$ = this.store.select(fromStore.selectSomeValue());
  // and some other logic...
}

在 HTML 中:

<div *ngIf="asyncProperty$ | async" class="testClass">...</div>

每次组件输入更改时将新的 Observable 分配给asyncProperty$变量是否安全? 我有一种奇怪的感觉,我应该避免这种方法,但不知道为什么。

详细说明@Input的评论,Angular 的更改检测将在@Input值更改时触发。 组件的生命周期中, Angular 将取消订阅传递给async管道的 Observables,在这种情况下,订阅已传递给async的新 Observable。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM