Angular 从服务接收已分解结构的对象时,ChangeDetectionStrategy.OnPush似乎不起作用?

Angular 从服务接收已分解结构的对象时,ChangeDetectionStrategy.OnPush似乎不起作用?,angular,typescript,angular-changedetection,Angular,Typescript,Angular Changedetection,我使用的是OnPush策略,我有一个更新一些数据的服务,当我在组件中收到数据时,我必须单击屏幕以查看屏幕上的更改 Parent.ts // Click start the logic click() { this.showChild = true; setTimeout(()=> this.update, 0); // Hack to show the loading in the component until it receives a va

我使用的是OnPush策略,我有一个更新一些数据的服务,当我在组件中收到数据时,我必须单击屏幕以查看屏幕上的更改

Parent.ts
    // Click start the logic
    click() {
       this.showChild = true;
       setTimeout(()=> this.update, 0); // Hack to show the loading in the component until it receives a value from the service
    }
    update() {
    this.explorer.getData().subscribe(data => {
      this.info = {...data};
      // this.cdr.detectChanges();
    });
  }
Parent html
<app-child *ngIf="showChild" [info]="info" [otherVar]="otherVar"></app-child>
Parent.ts
//单击开始逻辑
单击(){
this.showChild=true;
setTimeout(()=>this.update,0);//Hack显示组件中的加载,直到它从服务接收到值为止
}
更新(){
this.explorer.getData().subscribe(数据=>{
this.info={…data};
//this.cdr.detectChanges();
});
}
父html

我知道当使用分解结构时,会有一个引用更改,但我认为它不起作用。有人知道我做错了什么吗?

只有在@Input上使用OnPush更改检测策略时,引用更改才会触发组件上的更改检测。使用OnPush everywhere和

Parent.ts
//可观测变量声明
信息$:可观察;
//单击开始逻辑
单击(){
this.showChild=true;
setTimeout(()=>this.update,0);//Hack显示组件中的加载,直到它从服务接收到值为止
}
更新(){
//与其在这里订阅,不如只检索可观察到的内容
this.info$=this.explorer.getData();
}
父html

变量信息必须是另一个组件中的@input属性,才能获得.OnPush引用更改功能。我补充了答案。变量this.info我将其发送到子组件,但它不起作用。无法从此处复制该变量。你的应用程序中正在进行其他操作。您需要提供更多详细信息或在StackBlitz中重新创建错误。您是否可能已将changeDetectionStrategy从父组件更改为.onPush?这可以解释你的问题。我已经补充了答案。变量this.info我将其发送给子组件,但它不起作用。正如@Crazybutch所说,您的父组件的更改检测策略是什么?都是推送的,我认为问题是因为子组件非常重(这是一个有10个月的日历),我认为加载所需的时间比从服务接收响应所需的时间要长,我认为问题在于,我不知道如何处理此类情况您应该删除父级上的OnPush,因为它验证了我的答案:这..父级中订阅内的信息正在更改,OnPush策略不会在不手动触发更改检测的情况下更新其值。
Parent.ts
    // Observable Variable declaration
    info$: Observable<InfoModel>;
    // Click start the logic
    click() {
       this.showChild = true;
       setTimeout(()=> this.update, 0); // Hack to show the loading in the component until it receives a value from the service
    }
    update() {
    // instead of subscribing here, retrieve just the Observable
    this.info$ = this.explorer.getData();
  }
Parent html
<!--<app-child *ngIf="showChild" [info]="info" [otherVar]="otherVar"></app-child>-->
<app-child *ngIf="showChild" [info]="info$ | async" [otherVar]="otherVar"></app-child>