Angular @输入不适用于OnPush更改检测
page.htmlAngular @输入不适用于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
<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()
(有订阅或无订阅)。为什么它不起作用,因为我已为此noparcelDeliveryCost
创建了一个新的引用?我不喜欢手动方式。为什么引用更改在这里不起作用?如果您不喜欢“手动方式”,为什么要使用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()
(有订阅或无订阅)。为什么它不起作用,因为我已为此noparcelDeliveryCost
创建了一个新的引用?我不喜欢手动方式。为什么引用更改在这里不起作用?如果您不喜欢“手动方式”,为什么要使用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
});