为什么this.markerArray未定义?了解“这个”

时间:2019-01-30 11:17:47

标签: angular typescript this

我仍然对'this'有所了解。在以下情况下,我不断收到错误“ this.markerArray未定义”。但是我已经将markerArray声明为全局变量,所以我很困惑。

    @Component({
      selector: 'app-open-street-map',
      templateUrl: './open-street-map.component.html',
      styleUrls: ['./open-street-map.component.css']
    })
    export class OpenStreetMapComponent implements OnInit {
      @Output() private add = new EventEmitter();
      @Output() private edit = new EventEmitter<number>();
      artworkList: Artwork[];
      map;
    //declared it here
      markerArray = [];

constructor (){}

ngOnInit() {
    });

          buildMarkers(artworkList) {
   for (let artwork of artworkList) {
    const marker = this.buildPopup(artwork);
    this.markers.push(marker);
    }
    console.log("marker", marker);
  console.log("markerArray", this.markerArray);
  }
    }

我显然已从此ts页面中删除了很多代码!关键是要填充markerArray,我早些时候就做了初始化,但是由于某种原因,所有数组输入仍然“未定义”吗?

1 个答案:

答案 0 :(得分:4)

您需要使用空数组对其进行初始化

  markerArray = [];