Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.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
Javascript &引用;无法读取属性';选定区域';“未定义”的定义;在已初始化且不是空变量的_Javascript_Angular_Rxjs - Fatal编程技术网

Javascript &引用;无法读取属性';选定区域';“未定义”的定义;在已初始化且不是空变量的

Javascript &引用;无法读取属性';选定区域';“未定义”的定义;在已初始化且不是空变量的,javascript,angular,rxjs,Javascript,Angular,Rxjs,我有一个组件,它用ngOnInit中的选定区域过滤构建区域 rfid.component.ts @Component(...) export class RfidComponent implements OnInit { gridApi: GridApi; partList = new BehaviorSubject<IPart>(null); selectedZones: string[] = []; constructor(private zvSelectio

我有一个组件,它用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));
  }
}

@组件(…)
导出类RfidComponent实现OnInit{
gridApi:gridApi;
partList=新行为主体(空);
selectedZones:字符串[]=[];
构造函数(专用zvSelectionService:zvSelectionService){
}
isInBuildZone(bom:IBomLine):布尔值{
返回此.selectedZones.includes(bom.buildZone);
}
ngOnInit():void{

this.selectedZones=this.zvSelectionService.selectedZones.map(zone=>zone.name); console.log(this.selectedZones)/->[“LM3”] 零件清单。管道( map((list:IPart)=>({…list,bomLines:list.bomLines.filter(this.isInBuildZone)}), 过滤器((列表:IPart)=>list.bomLines.length>0), ) .subscribe(数据=>this.partList.next(数据)); } }
但是,即使selectedZones已初始化且不为空,我仍会收到以下错误:

ERROR TypeError: Cannot read property 'selectedZones' of undefined
    at isInBuildZone (rfid.component.ts:50)
    at Array.filter (<anonymous>)
    ...
错误类型错误:无法读取未定义的属性“selectedZones”
在isInBuildZone(rfid.component.ts:50)
在Array.filter()处
...

您应该将调用的函数绑定到组件类

map((list: IPart) => ({...list, bomLines: list.bomLines.filter(this.isInBuildZone.bind(this))})),

如果我必须根据提供的代码进行猜测,它应该位于
zvSelectionService.selectedZones
。错误表明
您正在从未定义的值读取“selectedZones”
。还有更多的代码吗。zvSelectionService.selectedZones仅从本地存储返回一个数组,因此它不是异步的。