【问题标题】:Angular - this.function is not a function [duplicate]Angular - this.function 不是函数[重复]
【发布时间】:2018-07-26 07:00:00
【问题描述】:

我对此有点绝望。我有一个组件,它获取数据并使用该数据的信息呈现地图。这里一切都好。我在这张地图上放了标记,我想在标记中做一个点击功能。这些函数调用组件中定义的另一个函数,并为我带来数据以在模式中显示它。但是,当我单击标记时,我收到此错误:

this.getInfoAlteracion 不是函数

我不知道为什么。我证明了很多事情,但我看不到错误。这是我的代码:

import { Component, OnInit } from '@angular/core';
import * as L from 'leaflet';
import 'leaflet.markercluster';
import { MapService } from './services';
import * as global from 'app/globals';
import { Alteracion } from './dto/alteracion';

@Component({
  selector: 'map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
  public alteraciones: Alteracion[] = [];
  public alteracion: Alteracion[] = [];

  constructor(private mapService: MapService) {}

  ngOnInit() {
    this.getAlteraciones();
  }


  getAlteraciones() {
    this.mapService.getListAlteraciones(11, 1).subscribe(
      result => {
        this.alteraciones = result;
        this.renderMap();
      },
      err => console.log(err)
    );
  }

  getInfoAlteracion(id_alteracion: string) {
    this.mapService.getInfoAlteracion(id_alteracion).subscribe(
      result => {
        this.alteracion = result;
        console.log(this.alteracion);
      },
      err => console.log(err)
    );
  }

  renderMap() {
    L.Icon.Default.imagePath = 'assets/img/theme/vendor/leaflet/';
    let map: any;

      map = L.map('map', {
            zoomControl: false,
            format: 'image/jpeg',
            center: L.latLng(40.4166395, -3.7046087),
            zoom: 2,
            minZoom: 0,
            maxZoom: 19,
            layers: [this.mapService.baseMaps.Esri]});

      L.control.zoom({ position: 'topleft' }).addTo(map);
      L.control.layers(this.mapService.baseMaps).addTo(map);

    let cluster = L.markerClusterGroup();

    for (let al of this.alteraciones) {
      let marker = L.marker([al.coory, al.coorx]);

      marker.on('click', function(e) {
        alert('Id alteración: ' + al.id_alteracion); // THIS WORKS
        this.getInfoAlteracion(al.id_alteracion); // THIS DON'T WORK
        console.log(this.alteracion);
      });

      cluster.addLayer(marker);
    }
    map.addLayer(cluster);   

}

有什么想法吗?提前致谢!

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您需要为事件处理程序使用箭头函数

      marker.on('click', (e) => {
        alert('Id alteración: ' + al.id_alteracion); // THIS WORKS
        this.getInfoAlteracion(al.id_alteracion); // THIS DON'T WORK
        console.log(this.alteracion);
      });
    

    在 Javascript 和 Typescript 中,thisfunction 的调用者确定。箭头函数从声明站点捕获this。很简单this 方式不是你所期望的。

    【讨论】:

      【解决方案2】:

      请这样尝试

      import { Component, OnInit } from '@angular/core';
      import * as L from 'leaflet';
      import 'leaflet.markercluster';
      import { MapService } from './services';
      import * as global from 'app/globals';
      import { Alteracion } from './dto/alteracion';
      
      @Component({
        selector: 'map',
        templateUrl: './map.component.html',
        styleUrls: ['./map.component.scss']
      })
      export class MapComponent implements OnInit {
        public alteraciones: Alteracion[] = [];
        public alteracion: Alteracion[] = [];
      
        constructor(private mapService: MapService) {}
      
        ngOnInit() {
          this.getAlteraciones();
        }
      
      
        getAlteraciones() {
          this.mapService.getListAlteraciones(11, 1).subscribe(
            result => {
              this.alteraciones = result;
              this.renderMap();
            },
            err => console.log(err)
          );
        }
      
        getInfoAlteracion(id_alteracion: string) {
          this.mapService.getInfoAlteracion(id_alteracion).subscribe(
            result => {
              this.alteracion = result;
              console.log(this.alteracion);
            },
            err => console.log(err)
          );
        }
      
        renderMap() {
            const reference=this; // code added
          L.Icon.Default.imagePath = 'assets/img/theme/vendor/leaflet/';
          let map: any;
      
            map = L.map('map', {
                  zoomControl: false,
                  format: 'image/jpeg',
                  center: L.latLng(40.4166395, -3.7046087),
                  zoom: 2,
                  minZoom: 0,
                  maxZoom: 19,
                  layers: [this.mapService.baseMaps.Esri]});
      
            L.control.zoom({ position: 'topleft' }).addTo(map);
            L.control.layers(this.mapService.baseMaps).addTo(map);
      
          let cluster = L.markerClusterGroup();
      
          for (let al of this.alteraciones) {
            let marker = L.marker([al.coory, al.coorx]);
      
            marker.on('click', function(e) {
              alert('Id alteración: ' + al.id_alteracion); 
              reference.getInfoAlteracion(al.id_alteracion); // code updated
              console.log(this.alteracion);
            });
      
            cluster.addLayer(marker);
          }
          map.addLayer(cluster);   
      
      }
      

      【讨论】:

      • 如果我们仍然使用 Typescript,为什么不直接使用箭头函数?
      猜你喜欢
      • 2017-08-18
      • 2019-05-30
      • 2020-03-22
      • 2020-06-26
      • 2020-12-18
      • 2020-06-06
      • 2021-11-22
      • 2018-04-10
      • 2019-08-22
      相关资源
      最近更新 更多