【问题标题】:Leaflet GeoJSON type not associating with GeoJSON object传单 GeoJSON 类型不与 GeoJSON 对象关联
【发布时间】:2020-10-14 02:16:30
【问题描述】:

我已经广泛使用 Leaflet 0.7,但我第一次尝试将 1.7 用于新项目。我正在尝试扩展 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) {
    ...
  }
}

编译器抱怨:

“GeoJSON”类型上不存在属性“initialize”

所以不知何故 GeoJSON 类型没有与 GeoJSON 对象相关联是我对此的解释......你的是什么?我正在使用 Leaflet 1.7.1、@types/leaflet 1.5.19 和 Typescript 4.0.3。我已经更新到 Angular 10 并删除并重新安装了 Leaflet 及其 @types 包,我还尝试清除我的节点模块并重新安装它们。有人知道我在这里做错了什么,对吗?

【问题讨论】:

    标签: angular typescript leaflet


    【解决方案1】:

    虽然 Leaflet JS 代码在其类中确实有一个 initialize 方法,并且在文档示例中提到了这个方法,但它并没有在 API 中明确描述,因此它的可能原因没有已包含在类型中...

    您可以添加它,或者更好地向@types/leaflet 包/存储库发送拉取请求,或者在使用initialize 方法的行上方使用// @ts-ignore 注释指令忽略TypeScript 编译器错误。

    【讨论】:

      【解决方案2】:

      你不想做这样的事情吗?

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

      我承认我不知道 Leaflet。但这是我的第一个猜测。

      【讨论】:

        猜你喜欢
        • 2015-06-28
        • 2015-08-11
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多