简体   繁体   English

设置类属性的事件监听器

[英]Setting event listener of a class property

I'm using Leaflet and Leaflet-geoman in conjunction with wfs-t to create an editable map object.我将 Leaflet 和 Leaflet-geoman 与 wfs-t 结合使用来创建可编辑的地图对象。 I have a class EditMap that has a leaflet map as a property.我有一个类 EditMap,它有一个传单地图作为属性。 I'm trying to listen for the 'pm:create' event on this map for every class.我正在尝试为每个班级在此地图上收听 'pm:create' 事件。 Here is my code:这是我的代码:

class EditMap {
    constructor(map){
        this.map = map;//Leaflet map
    }
    this.map.on('pm:create', e => {
        console.log('Feature created');
    });
}

I get the error Uncaught SyntaxError: Unexpected token '.'我收到错误 Uncaught SyntaxError: Unexpected token '.' on this line:在这一行:

this.map.on('pm:create', e => {

I expect I'm missing something simple.我希望我错过了一些简单的东西。 My basic questions boils down to: How do you listen for an event on an object property?我的基本问题归结为:如何监听对象属性上的事件?

In the wrong place在错误的地方

You're using a class so you could do:您正在使用一个类,因此您可以执行以下操作:

class EditMap {
  constructor(map) {
    this.map = map;

    this.map.on('pm:create', this.pmCreate)
  }

  pmCreate(e) {
    console.log('Feature created');
  }
}

or simply this;或者干脆这个; but will fill out quickly as you add other listerners:但会在您添加其他听众时快速填写:

class EditMap {
  constructor(map) {
    this.map = map; //Leaflet map

    this.map.on('pm:create', e => {
      console.log('Feature created');
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM