简体   繁体   English

传单 GeoJSON 类型未与 GeoJSON 对象关联

[英]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.我已经广泛使用 Leaflet 0.7,但我第一次为一个新项目尝试 1.7。 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:我正在尝试扩展 GeoJSON 图层类以使用默认的pointToLayer方法,这些似乎失败了,因为 GeoJSON 的类型定义没有与对象关联......这是我正在尝试的:

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' “GeoJSON”类型上不存在属性“初始化”

So somehow the GeoJSON type isn't being associated with the GeoJSON object is my interpretation of this... What's yours??所以不知何故 GeoJSON 类型没有与 GeoJSON 对象相关联是我对此的解释......你的是什么? I'm using Leaflet 1.7.1, @types/leaflet 1.5.19, and Typescript 4.0.3.我正在使用 Leaflet 1.7.1、@types/leaflet 1.5.19 和 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.我已经更新到 Angular 10 并删除并重新安装了 Leaflet 和它的 @types 包,我还尝试清除我的节点模块并重新安装它们。 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...虽然 Leaflet JS 代码确实在其类上有一个initialize方法,并且在文档示例中提到了这个方法,但它并没有在 API 中明确描述,因此它没有被包含在类型...

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.您可以添加它,或者更好地向@types/leaflet包/repo 发送拉取请求,或者简单地忽略 TypeScript 编译器错误,并在使用initialize方法的行上方使用// @ts-ignore注释指令。

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.但这将是我的第一个猜测。

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

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