代码之家  ›  专栏  ›  技术社区  ›  solidau

将活页标记扩展名转换为Typescript

  •  1
  • solidau  · 技术社区  · 3 年前

    import L from 'leaflet';
    
    L.Marker.MyMarker= L.Marker.extend({}).addInitHook(function (this: ILazyMarker) {
        this.on(
            'add',
            function (this: ILazyMarker) {
                this._updateIconVisibility = function (this: ILazyMarker) {
                    var map = this._map,
                        isVisible = map.getBounds().contains(this.getLatLng()),
                        wasVisible = this._wasVisible,
                        icon = this._icon,
                        iconParent = this._iconParent,
                        shadow = this._shadow,
                        shadowParent = this._shadowParent;
    
                    // remember parent of icon
                    if (!iconParent) {
                        iconParent = this._iconParent = icon.parentNode;
                    }
                    if (shadow && !shadowParent) {
                        shadowParent = this._shadowParent = shadow.parentNode;
                    }
    
                    // add/remove from DOM on change
                    if (iconParent != null && isVisible != wasVisible) {
                        if (isVisible) {
                            iconParent.appendChild(icon);
                            if (shadowParent != null && shadow) {
                                shadowParent.appendChild(shadow);
                            }
                        } else {
                            iconParent.removeChild(icon);
                            if (shadowParent != null && shadow) {
                                shadowParent.removeChild(shadow);
                            }
                        }
    
                        this._wasVisible = isVisible;
                    }
                };
    
                // on map size change, remove/add icon from/to DOM
                this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
                this._updateIconVisibility();
            },
            this,
        );
    });
    

    这工作得很好,但为了在项目中保持一致性,我宁愿使用Typescript类。以下是我的尝试:

    import L from 'leaflet';
    
    export default class MyMarker extends L.Marker {
        _wasVisible!: boolean;
        _icon!: any;
        _shadowParent!: any;
        _iconParent!: any;
    
        constructor(latlng: L.LatLngExpression, options?: L.MarkerOptions) {
            super(latlng, options);
        }
    
        onAdd(): this {
            this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
            this._updateIconVisibility();
            return this;
        }
    
        _updateIconVisibility() {
            var map = this._map,
                isVisible = map.getBounds().contains(this.getLatLng()),
                wasVisible = this._wasVisible,
                icon = this._icon,
                iconParent = this._iconParent,
                shadow = this._shadow,
                shadowParent = this._shadowParent;
    
            // remember parent of icon
            if (!iconParent) {
                iconParent = this._iconParent = icon.parentNode;
            }
            if (shadow && !shadowParent) {
                shadowParent = this._shadowParent = shadow.parentNode;
            }
    
            // add/remove from DOM on change
            if (iconParent != null && isVisible != wasVisible) {
                if (isVisible) {
                    iconParent.appendChild(icon);
                    if (shadowParent != null && shadow) {
                        shadowParent.appendChild(shadow);
                    }
                } else {
                    iconParent.removeChild(icon);
                    if (shadowParent != null && shadow) {
                        shadowParent.removeChild(shadow);
                    }
                }
    
                this._wasVisible = isVisible;
            }
        }
    }
    

    当我尝试使用基于类的标记时,对于我添加的每个标记都会出现此错误,并且贴图将不会渲染:

    TypeError: Cannot read property 'parentNode' of undefined
        at MyMarker._updateIconVisibility (leaflet-my-marker.ts:78)
        at MyMarker.onAdd (leaflet-my-marker.ts:63)
        at MyMarker._layerAdd (leaflet-src.js:6567)
        at NewClass.whenReady (leaflet-src.js:4428)
        at NewClass.addLayer (leaflet-src.js:6629)
        at NewClass.addLayer (leaflet-src.js:6780)
        at VueComponent.addLayer (LLayerGroup.js:164)
        at VueComponent.FirecamMarker.mounted (MyMarker.vue?6277:99)
        at invokeWithErrorHandling (vue.runtime.esm.js:1854)
        at callHook (vue.runtime.esm.js:4219)
    
    1 回复  |  直到 3 年前
        1
  •  2
  •   solidau    3 年前

    提议的“StackOverflow法则”:不管你在一段代码上工作了多长时间,你都无法解决这个问题,除非你把它发布到StackOverflow上

    我已经断断续续地做了好几天了。。。当然,我能够解决它后,立即张贴我的问题。我要打电话给警察 super onAdd 我的方法 添加

    onAdd(): this {
        super.onAdd(this._map);
        this._map.on('resize moveend zoomend', this._updateIconVisibility, this);
        this._updateIconVisibility();
        return this;
    }
    

    .addInitHook() .setInitHook()