[英]Change Detection Strategy onpush not working
Parent.component.html父组件.html
<app-child [activeUser]="activeUser" *ngIf="eceConfirm && activeUser"></app-child>
Parent.component.ts In ngOnInit I called getAllEmployees to get all the employee data and passed 0th index data to the child component using @Input() activeUser. Parent.component.ts在 ngOnInit 中,我调用 getAllEmployees 来获取所有员工数据,并使用 @Input() activeUser 将第 0 个索引数据传递给子组件。
getAllEmployees() {
this.service
.getCommonEmployeesDetail(this.user["uid"], this.selectedRatingCycleId)
.subscribe((res) => {
this.userList = res;
this.changeUser(this.userList[0]);
});
}
changeUser(user) {
console.log("user", user);
this.activeUser=user;
}
Child.component.ts子组件.ts
I have implemented changeDetection.Onpush strategy in my child component.我已经在我的子组件中实现了 changeDetection.Onpush 策略。 After getting my activeUser data I am passing it in the changeChildUser() method to fetch data from a request and assigning it to the this.cycleData.
获取我的 activeUser 数据后,我将它传递到 changeChildUser() 方法中以从请求中获取数据并将其分配给 this.cycleData。
The problem which I am facing 1.When I try to print {{cycleData.Englishmarks}} in HTML page doesn't get refreshed.我面临的问题 1.当我尝试在 HTML 页面中打印 {{cycleData.Englishmarks}} 时没有刷新。 I consoled this.cycleData and it was displaying the value in the console.
我安慰了 this.cycleData 并且它在控制台中显示了该值。
Any idea what might be the problem.Any input is appreciated.Thanks in advance任何想法可能是什么问题。感谢任何输入。提前致谢
@Input() activeUser: BpuData;
ngOnChanges(changes: SimpleChanges) {
this.changeChildUser();
}
changeChildUser() {
this.chapterService.getcycle(this.activeUser).subscribe(response =>{
this.cycleData=response;
});
}
That's because detection is changed only when an input is changed in the OnPush
components.这是因为只有在
OnPush
组件中更改输入时才会更改检测。
You have to either detect changed manually:您必须手动检测更改:
changeChildUser() {
this.chapterService.getcycle(this.activeUser).subscribe(response =>{
this.cycleData=response;
this.cdr.detectChanges();
});
}
Or you async pipe
as @Maxime already commented.或者你
async pipe
因为@Maxime 已经评论过。
PS: Always remember to unsubscribe on the destroy. PS:永远记得在销毁时取消订阅。
The best solution for OnPush
components is to use markForCheck()
. OnPush
组件的最佳解决方案是使用markForCheck()
。 detectChanges()
also solves the problem, but it can create a performance problem since it triggers other components in the tree. detectChanges()
也解决了这个问题,但它会造成性能问题,因为它会触发树中的其他组件。
https://angular.io/api/core/ChangeDetectorRef#markForCheck https://angular.io/api/core/ChangeDetectorRef#markForCheck
constructor(private cd: ChangeDetectorRef) {}
this.chapterService.getcycle(this.activeUser).subscribe(response => {
this.cycleData = response;
this.cd.markForCheck();
});
I prepared an example close to your project.我准备了一个接近你项目的例子。
https://stackblitz.com/edit/angular-ivy-duuj5y https://stackblitz.com/edit/angular-ivy-duuj5y
If you want to understand exactly what happened, here's a very good explanation.如果你想确切地了解发生了什么,这里有一个很好的解释。
https://stackoverflow.com/a/41364469/6478359 https://stackoverflow.com/a/41364469/6478359
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.