Angular 我应该订阅或使用备份属性来更新组件中的依赖数据吗?

Angular 我应该订阅或使用备份属性来更新组件中的依赖数据吗?,angular,typescript,observable,Angular,Typescript,Observable,我很难弄清楚什么是最佳实践 我有以下问题: <div> <app-child [data]="data | async"> </app-child> </div> B:使用可观察对象和订阅 <div> <!-- pass down the whole observable --> <app-child [data]="data"> </app-child> <

我很难弄清楚什么是最佳实践

我有以下问题:

<div>
   <app-child [data]="data | async"> 
   </app-child>
</div>
B:使用可观察对象和订阅

<div>
   <!-- pass down the whole observable -->
   <app-child [data]="data"> 
   </app-child>
</div>

类ChildComponent实现OnInit{
@输入数据:可观察;
标签颜色:字符串;
onInit(){
这是数据
.subscribe(值=>{
this.labemColor=this.labelService
.getLabelColor(值.warningPeriod)
}) 
}
//建造师
}
什么更好

当我订阅时,我需要取消订阅,这是“恼人的”


在哪种情况下我应该做什么?

如果您不必修改异步数据,
AsyncPipe
是最好的选择,因为您不必记住取消订阅(避免内存泄漏)并在后台检测更改

如果您必须修改数据,请在获取新数据后执行一些操作。在这种情况下,订阅。ts更好,但您必须记住取消订阅以及设置
ChangeDetectionStrategy.OnPush
时必须运行
detectChanges

因此,在大多数情况下,您应该使用
AsyncPipe
,只有在您真正需要时才订阅

<div>
   <!-- pass down the whole observable -->
   <app-child [data]="data"> 
   </app-child>
</div>
class ChildComponent implements OnInit{
     @Input data: Observable<object>;
      labelColor: String;
      onInit(){
         this.data
         .subscribe( value =>{
              this.labemColor= this.labelService
.getLabelColor(value.warningPeriod)
          }) 
      }
      //constructor

}