javascript - Angular @Input() 不更新子用户界面
问题描述
我已经实现了一个子组件来根据通过@Input() 提供的列表来呈现表格。数据是通过 http 加载的,但是 UI(子组件)不会更新,除非我在屏幕上挥动鼠标。我已经看到人们发布了关于在我的孩子中实现 ngOnChanges() 的帖子,但我认为 Angular 应该默认执行此操作?我错过了什么吗?为什么 UI 不会随之更新?
子代码如下所示:
child.component.ts
@Component({
selector: 'child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss'],
})
export class ChildComponent implements {
@Input() data: any[] = [];
constructor() {}
}
child.component.html
<table>
<tr *ngFor="let item of data"><td>{{ item }}</td></tr>
</table>
使用该组件的父代码如下所示:
父组件.ts
@Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss'],
})
export class ParentComponent implements OnInit {
data: string[] = [];
constructor(private endpointService: EndpointService) {}
ngOnInit() {
// response is a string array like: ['hello', 'world']
this.endpointService.loadData().subscribe((response) => {
this.data = response;
});
}
}
父组件.html
<child [data]="data"></child>
============================== 编辑==================== ===============
我验证了它仅在订阅回调内部更新时加载失败(如果我设置了一个静态数组,它加载就好了)。
所以看起来我可以通过在父组件中运行 changeDetectorRef.detectChanges() 来解决这个问题,但这感觉就像我不应该这样做一样。这是解决这个问题的好方法吗?或者这是否表明我的实现有问题?
父组件.ts
@Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss'],
})
export class ParentComponent implements OnInit {
data: string[] = [];
constructor(private endpointService: EndpointService,
private changeDetectorRef: ChangeDetectorRef) {}
ngOnInit() {
// response is a string array like: ['hello', 'world']
this.endpointService.loadData().subscribe((response) => {
this.data = response;
this.changeDetectorRef.detectChanges();
});
}
}
解决方案
您还可以尝试通过例如扩展运算符强制值引用更新来强制更改检测:
this.endpointService.loadData().subscribe((response) => {
this.data = [...response];
});
推荐阅读
- r - 栅格的相关性和 p 值
- c++ - 如何在 C++ 中通过 UDP 发送任何文件(图像、exe)?
- php - Codeigniter 4中的调试工具栏不起作用
- python - 如何在 Python 中进行实时语音活动检测?
- javascript - {props.children} 与普通对象有何不同?
- laravel - 我对 OpenTok 和 Nexmo API 有疑问
- google-places-api - Google Places 自动填充 API 限制
- c# - 当我抛出 ac# 异常时,IIS 日志中的 Http 500
- html - 以图像为中心的时间轴
- python - 我是否需要安装 Hadoop 才能使用 Pyspark 的各个方面?