简体   繁体   中英

Leaflet GeoJSON type not associating with GeoJSON object

I've used Leaflet 0.7 extensively, but am trying 1.7 for a new project for the first time. I'm trying to extend the GeoJSON layer class to use a default pointToLayer method, these appears to fail because the type definition for GeoJSON isn't being associated with the object... Here's what I'm trying:

import { GeoJSON } from 'leaflet';

const geojsonMarkerOptions = {
    ...
};

export const Layer = GeoJSON.extend({
  initialize: function() {

    GeoJSON.prototype.initialize.call(this, [], {
      pointToLayer: (feature, latlng) => this.pointToLayer(feature, latlng)
    });
  },

  pointToLayer: function(feature, latlng) {
    ...
  }
}

And the compiler complains:

Property 'initialize' does not exist on type 'GeoJSON'

So somehow the GeoJSON type isn't being associated with the GeoJSON object is my interpretation of this... What's yours?? I'm using Leaflet 1.7.1, @types/leaflet 1.5.19, and Typescript 4.0.3. I've updated to Angular 10 and removed and re-installed both Leaflet and its @types package, and I've also tried clearing out my node modules and re-installing them. Somebody knows what I'm doing wrong here, right??

While it is true that Leaflet JS code does have an initialize method on its classes, and that this method is mentioned in the documentation examples, it is nevertheless not explicitly described in the API, hence the probable reason for it not having been included in the types...

You could either add it, or better send a Pull Request to the @types/leaflet package/repo, or simply ignore the TypeScript compiler error with // @ts-ignore comment directive just above the line where you use the initialize method.

Wouldn't you want to do something like this?

export class Layer extends GeoJSON {
   initialize() {
      super.initialize()
   }
}

I admit not knowing Leaflet though. But this would be my first guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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