无法在打字稿中访问全局变量
Cannot access global variable in typescript
我在代码中遇到了一个奇怪的问题。我在 class 中声明了 markers 变量,所以它是全局的,可以在 class 中的任何地方访问。但问题是我可以访问 initMap 内部的标记,但无法在 InitMap 的函数内部访问它。错误提示:TS2339 属性 'markers' 在类型 void
上不存在
export class StudentGraphicReportMapComponent implements OnInit {
locations: any[] = [];
markers:any[] = [];
constructor(private http: Http, private elementRef: ElementRef , private dashboardService: DashboardService) {
}
initMap() {
// ------ CAN ACCESS markers here
this.locations.forEach(function(location: Location) {
for ( let b = 0; b < location.studentCount; b++) {
let marker = new google.maps.Marker({
position: location,
label: 'A'
});
// ----------CANNOT ACCESS markers here
this.markers.push(marker); //Error:Property 'markers' does not exist on type void
}
});
}
ngOnInit() {
this.dashboardService.getStudentCoordinates().then(data => {
this.locations = data.data;
this.initMap();
});
}
}
请帮我解决这个问题。亲切的问候
您应该像这样使用 arrow functions 访问 this
:
export class StudentGraphicReportMapComponent implements OnInit {
locations: any[] = [];
markers:any[] = [];
constructor(private http: Http, private elementRef: ElementRef , private dashboardService: DashboardService) {
}
initMap() {
this.locations.forEach((location: Location) => {
for ( let b = 0; b < location.studentCount; b++) {
let marker = new google.maps.Marker({
position: location,
label: 'A'
});
this.markers.push(marker); //Error:Property 'markers' does not exist on type void
}
});
}
ngOnInit() {
this.dashboardService.getStudentCoordinates().then(data => {
this.locations = data.data;
this.initMap();
});
}
}
让局部变量指向这个
initMap() {
let _self = this;
...
}
并在函数
中使用_self.markers
我在代码中遇到了一个奇怪的问题。我在 class 中声明了 markers 变量,所以它是全局的,可以在 class 中的任何地方访问。但问题是我可以访问 initMap 内部的标记,但无法在 InitMap 的函数内部访问它。错误提示:TS2339 属性 'markers' 在类型 void
上不存在 export class StudentGraphicReportMapComponent implements OnInit {
locations: any[] = [];
markers:any[] = [];
constructor(private http: Http, private elementRef: ElementRef , private dashboardService: DashboardService) {
}
initMap() {
// ------ CAN ACCESS markers here
this.locations.forEach(function(location: Location) {
for ( let b = 0; b < location.studentCount; b++) {
let marker = new google.maps.Marker({
position: location,
label: 'A'
});
// ----------CANNOT ACCESS markers here
this.markers.push(marker); //Error:Property 'markers' does not exist on type void
}
});
}
ngOnInit() {
this.dashboardService.getStudentCoordinates().then(data => {
this.locations = data.data;
this.initMap();
});
}
}
请帮我解决这个问题。亲切的问候
您应该像这样使用 arrow functions 访问 this
:
export class StudentGraphicReportMapComponent implements OnInit {
locations: any[] = [];
markers:any[] = [];
constructor(private http: Http, private elementRef: ElementRef , private dashboardService: DashboardService) {
}
initMap() {
this.locations.forEach((location: Location) => {
for ( let b = 0; b < location.studentCount; b++) {
let marker = new google.maps.Marker({
position: location,
label: 'A'
});
this.markers.push(marker); //Error:Property 'markers' does not exist on type void
}
});
}
ngOnInit() {
this.dashboardService.getStudentCoordinates().then(data => {
this.locations = data.data;
this.initMap();
});
}
}
让局部变量指向这个
initMap() {
let _self = this;
...
}
并在函数
中使用_self.markers