javascript - “无法读取未定义的属性‘selectedZones’”已初始化且非空变量
问题描述
我有一个组件使用 ngOnInit 中的选定区域过滤构建区域
rfid.component.ts
@Component(...)
export class RfidComponent implements OnInit {
gridApi: GridApi;
partList = new BehaviorSubject<IPart>(null);
selectedZones: string[] = [];
constructor(private zvSelectionService: ZvSelectionService) {
}
isInBuildZone(bom: IBomLine): boolean {
return this.selectedZones.includes(bom.buildZone);
}
ngOnInit(): void {
this.selectedZones = this.zvSelectionService.selectedZones.map(zone => zone.name);
console.log(this.selectedZones) // -> ["LM3"]
of(partList).pipe(
map((list: IPart) => ({...list, bomLines: list.bomLines.filter(this.isInBuildZone)})),
filter((list: IPart) => list.bomLines.length > 0),
)
.subscribe(data => this.partList.next(data));
}
}
但我收到以下错误,即使 selectedZones 已初始化且不为空:
ERROR TypeError: Cannot read property 'selectedZones' of undefined
at isInBuildZone (rfid.component.ts:50)
at Array.filter (<anonymous>)
...
解决方案
您应该将调用的函数绑定到组件类
map((list: IPart) => ({...list, bomLines: list.bomLines.filter(this.isInBuildZone.bind(this))})),
推荐阅读
- python - 多个集合中的聚合查询 Mongodb (pymongo)
- javascript - 如何使用 jicai q2 POS 热敏打印机 (商米 V1) 使用基于 asp.net 的 web 应用程序?
- r - 访问有关社会问题、选举或政治的 Facebook 广告库
- excel - 为字母分配数值,按列计数
- r - 尝试创建函数时 defaultCluster(cl) 出错
- android - 如何在一个脚本上为两个操作系统(Android 和 IOS)创建驱动程序
- java - 在应用程序重新启动之前,Android RecyclerView 不会加载 Firebase 项目
- qt - 如何在 QtOpenGLWindow 上绘制 3d 文本?
- python-3.x - 浏览表中的记录
- azure - 在 Azure Blob 容器中创建两个文件时,如何在 Azure 数据工厂中创建事件触发器?