Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular @输入不适用于OnPush更改检测_Angular_Typescript_Ionic Framework_Rxjs_Angular Changedetection - Fatal编程技术网

Angular @输入不适用于OnPush更改检测

Angular @输入不适用于OnPush更改检测,angular,typescript,ionic-framework,rxjs,angular-changedetection,Angular,Typescript,Ionic Framework,Rxjs,Angular Changedetection,page.html <app-parcel-delivery-cost-promo [parcelDeliveryCost]="parcelDeliveryCost"> </app-parcel-delivery-cost-promo> componet.html <ion-grid> <ion-row> <ion-col size="12" > <h5 class="font-bold margin

page.html

<app-parcel-delivery-cost-promo [parcelDeliveryCost]="parcelDeliveryCost">
  </app-parcel-delivery-cost-promo>
componet.html

<ion-grid>
  <ion-row>
    <ion-col size="12" >
      <h5 class="font-bold margin-top-bottom-5">{{parcelDeliveryCost?.title}}</h5>
    </ion-col>
    <ion-col size="12">
      <div class="color-medium">{{parcelDeliveryCost?.description}}</div>
    </ion-col>
    <ion-col size="12">
      <h4 class="margin-top-bottom-5 text-line-through">{{parcelDeliveryCost?.cost}}</h4>
    </ion-col>

  </ion-row>
</ion-grid>

{{parcelDeliveryCost?.title}
{{parcelDeliveryCost?.description}
{{parcelDeliveryCost?.cost}

问:你能告诉我为什么
@Input
不能与
OnPush
更改检测一起工作吗?这个代码的问题在哪里?i、 e.我看不到
应用程序包裹交付成本促销
组件。但是如果我要在页面上更改某些内容,它就会显示。

输入工作正常。使用
OnPush
ngOnInit
中订阅不会运行更改检测。您需要执行以下操作之一:

  • 在订阅中手动运行更改检测器
  • 使用
    async
    管道而不是
    .subscribe()
    (异步管道运行
    markForCheck
  • 关闭
    OnPush
    策略

输入工作正常。使用
OnPush
ngOnInit
中订阅不会运行更改检测。您需要执行以下操作之一:

  • 在订阅中手动运行更改检测器
  • 使用
    async
    管道而不是
    .subscribe()
    (异步管道运行
    markForCheck
  • 关闭
    OnPush
    策略
试试这个

 this.parcelDeliveryCost = Object.assign({},{
        title: res['Client.Parcel-Delivery-Cost'],
        description: res['Client.Parcel-Picked-Up-From-The-Location-And-Delivered-To-You'],
        cost: environment.parcelDeliveryCost
      });
就好像对象的参考已更新一样,angular将运行更改检测。

尝试此操作

 this.parcelDeliveryCost = Object.assign({},{
        title: res['Client.Parcel-Delivery-Cost'],
        description: res['Client.Parcel-Picked-Up-From-The-Location-And-Delivered-To-You'],
        cost: environment.parcelDeliveryCost
      });

就好像对象的引用被更新了一样,angular将运行更改检测。

我已删除该子对象,并将其如此放置。但它仍然不起作用。有线索吗<代码>this.parcelDeliveryCost={title:'packet Delivery Cost',description:'packet从该位置提取并交付给您',成本:environment.parcelDeliveryCost,promoCost:environment.parcelDeliveryCostPromo,promoText:'Promo',}上述代码也不会运行更改检测。如果要“测试”输入是否正常工作,请定义
this.parcelDeliveryCost
外部
ngOnInit
。在变量定义期间填充值。您还可以在变量值赋值后注入
ChangeDetectorRef
并运行
detectChanges()
(有订阅或无订阅)。为什么它不起作用,因为我已为此no
parcelDeliveryCost
创建了一个新的引用?我不喜欢手动方式。为什么引用更改在这里不起作用?如果您不喜欢“手动方式”,为什么要使用onPush。。?没有道理。@MikeOne它必须有一个自然的方式来做到这一点。e、 g.
async
pipe等。这就是为什么我不喜欢那个丑陋的黑客。我想知道如何在我的用例中使用
async
管道。我已经删除了该子模块,并将其放在这里。但它仍然不起作用。有线索吗<代码>this.parcelDeliveryCost={title:'packet Delivery Cost',description:'packet从该位置提取并交付给您',成本:environment.parcelDeliveryCost,promoCost:environment.parcelDeliveryCostPromo,promoText:'Promo',}上述代码也不会运行更改检测。如果要“测试”输入是否正常工作,请定义
this.parcelDeliveryCost
外部
ngOnInit
。在变量定义期间填充值。您还可以在变量值赋值后注入
ChangeDetectorRef
并运行
detectChanges()
(有订阅或无订阅)。为什么它不起作用,因为我已为此no
parcelDeliveryCost
创建了一个新的引用?我不喜欢手动方式。为什么引用更改在这里不起作用?如果您不喜欢“手动方式”,为什么要使用onPush。。?没有道理。@MikeOne它必须有一个自然的方式来做到这一点。e、 g.
async
pipe等。这就是为什么我不喜欢那个丑陋的黑客。我想知道如何在我的用例中使用
async
管道。
 this.parcelDeliveryCost = Object.assign({},{
        title: res['Client.Parcel-Delivery-Cost'],
        description: res['Client.Parcel-Picked-Up-From-The-Location-And-Delivered-To-You'],
        cost: environment.parcelDeliveryCost
      });