【问题标题】:Ionic leaflet moveend triggered function not found找不到离子传单 moveend 触发功能
【发布时间】:2019-02-18 06:58:06
【问题描述】:

我目前正在使用传单进行 ionic 3 项目。 我想在 moveend(或 dragend)之后触发一个函数,但它不会重新调整我正在触发的函数。它说它没有被声明。

我得到的错误:

'this.functiontoload 不是函数'。

希望有人能够提供帮助。

loadmap(){
    this.map = L.map('map', {rotate: true, touchRotate: true, zoomControl: false}).fitWorld();
    L.tileLayer('https://maps.tilehosting.com/styles/streets/{z}/{x}/{y}.png?key=XXXX', {
        attribution: 'Projectname',
        crossOrigin: true
    }).addTo(this.map);


    this.map.locate({setView: true, maxZoom: 20});

    this.userMarker = L.marker([51.5, -0.09], {icon: this.userMarkerIcon}).addTo(this.map);

    let watch = this.geolocation.watchPosition();
    watch.subscribe((data) => {
        let latitude = data.coords.latitude;
        let longitude = data.coords.longitude;

        var newPosition = L.latLng(latitude, longitude);

        this.userMarker.setLatLng(newPosition);
    });


    this.map.on('moveend', function() {
        this.functiontoload();
    });

}

functiontoload() {
....
}

【问题讨论】:

标签: angular typescript ionic-framework leaflet dom-events


【解决方案1】:

它只是关于“这个”。在您的回调函数中的含义,它与传单无关。 您可以使用箭头功能或将“this”传递给您的功能 因此您可以将代码更改为:

 this.map.on('moveend', ()=> {
    this.functiontoload();
});

箭头函数的动机是:

-你不需要一直打字功能

-从词法上捕捉this的意思

-它在词法上捕获参数的含义

【讨论】:

  • 虽然这个答案可能会解决问题。只提供解决方案对任何人都没有帮助。 OP 的代码有什么不正确的地方,为什么你的例子能解决这个问题?
  • 我遇到了同样的问题,如果你不使用 lambda "this",那是关于 "=>" 的。是未定义的。问题是关于角度而不是传单。
  • 一个 lambda 只是一个匿名函数(包括普通的 function () {} 定义)。这特别是关于 ES6 中的新 arrow function。您的解释应该添加到答案中,而不是作为对我的评论的答案。请记住,这样做的目的是让阅读您的答案的人理解为什么这样做(在问题的上下文中),因此他们以后不会再偶然发现同样的问题。
  • @JohanWentholt。我添加了一些额外的信息来回答。
猜你喜欢
  • 2020-12-29
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多