开发者问题收集

react-leaflet-rotatedmarkers:TypeError:Super 表达式必须为空或为函数,而不是对象

2018-12-14
1503

正如您在主题中看到的,我的问题是,当我想导入 react-leaflet-rotatedmarker 时,仅导入,我的 react webapplication 就会向我抛出此消息。

我正在使用 react-leaflet v2.1.2。

这是我的 js 代码片段:

import React, { Component } from 'react';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
import L, { map, addTo } from 'leaflet';
import RotatedMarker from 'react-leaflet-rotatedmarker'
import './App.css'

//importing marker/icon
var ego_veh_icon = L.icon({
    iconUrl: require('./design/icons/ego_veh_arrow.svg'),
    iconSize: [150, 200],
    iconAnchor: [82.5, 40],
    popupAnchor: [0, -25],
});

var av_veh_icon = L.icon({
    iconUrl: require('./design/icons/autonom_veh_arrow.svg'),
    iconSize: [150, 200],
    iconAnchor: [82.5,55],
    popupAnchor: [-5, -25],
});

var nav_veh_icon = L.icon({
    iconUrl: require('./design/icons/non_autonom_veh_arrow.svg'),
    iconSize: [150, 200],
    iconAnchor: [82.5, 60],
    popupAnchor: [-10, -25],
});

class Map_hmi extends Component {
    constructor() {
        super();
        this.state = {
            markers: [[x, y]],
            param: null,
        };
    }

    // ego veh position 
    ego_veh = {
        lat: x,
        lng: y,
    }

     // nav veh position 
     nav_veh = {
        lat: x,
        lng: y,
    }

     // av veh1 position 
     av_veh1 = {
        lat: x,
        lng: y,
    }

    // av veh2 position 
    av_veh2 = {
        lat: x
        lng: y,
    }

    render() {
        
        const ego_veh_pos = [this.ego_veh.lat, this.ego_veh.lng]
        const nav_veh_pos = [this.nav_veh.lat, this.nav_veh.lng]
        const av_veh1_pos = [this.av_veh1.lat, this.av_veh1.lng]
        const av_veh2_pos = [this.av_veh2.lat, this.av_veh2.lng]
        return (
            // declaring the map
            <Map
                className="map"
                center={ego_veh_pos}
                zoom={15}
                zoomControl={false}
            >
                <TileLayer
                    attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />  
                    {/* <RotatedMarker position={position} rotationAngle={180} rotationOrigin={'center'} /> */}
                    {/* ego veh */}
                    <Marker 
                        position={ego_veh_pos}
                        icon= {ego_veh_icon}>
                        <Popup>
                        EGO <br/>
                        </Popup>
                    </Marker>
                    {/* nav veh */}
                    <Marker 
                        position={nav_veh_pos}
                        icon= {nav_veh_icon}>
                        <Popup>
                        NAV <br/>
                        </Popup>
                    </Marker>
                    {/* av veh1 */}
                    <Marker 
                        position={av_veh1_pos}
                        icon= {av_veh_icon}>
                        <Popup>
                        AV <br/>
                        </Popup>
                    </Marker>
                     {/* av veh2 */}
                     <Marker 
                        position={av_veh2_pos}
                        icon= {av_veh_icon}>
                        <Popup>
                        AV <br/>
                        </Popup>
                    </Marker>
                )}
            </Map>
            
        );
    }
}

export default Map_hmi;

所以我的代码在没有导入的情况下工作正常,但是只要我导入它:

TypeError: Super expression must either be null or a function, not object

我只想旋转图标....

有什么帮助吗?

问候

2个回答

提供的另一个答案 似乎是正确的, react-leaflet-rotatedmarkerreact-leaflet v2 包不兼容。

对于 react-leaflet v2 库, RotatedMarker 组件可以像这样实现( 提供与 react-leaflet-rotatedmarker 包相同的行为 ):

import React, { Component } from "react";
import { withLeaflet } from "react-leaflet";
import { Marker } from "react-leaflet";
import { Marker as LeafletMarker } from 'leaflet-rotatedmarker';

const RotatedMarker = props => {
  const setupMarker = marker => {
    if (marker) {
      if (props.rotationAngle)
        marker.leafletElement.setRotationAngle(props.rotationAngle);
      marker.leafletElement.setRotationOrigin(props.rotationOrigin);
    }
  };

  return <Marker ref={el => setupMarker(el)} {...props} />;
};

RotatedMarker.defaultProps = {
  rotationOrigin: "center"
};

export default withLeaflet(RotatedMarker);

演示页面

Vadim Gremyachev
2018-12-17

tldr:使用此评论中的代码应该可以解决您的问题。 https://github.com/verdie-g/react-leaflet-rotatedmarker/issues/1#issuecomment-427285940

更长的答案: react-leaflet v2 在组件扩展方式上与 v1 相比有非常重大的突破性变化。如果您查看 react-leaflet-rotatedmarker 的代码,您会看到以下行: https://github.com/verdie-g/react-leaflet-rotatedmarker/blob/master/src/RotatedMarker.jsx#L5

Marker 类的扩展在 v2 中不起作用,这就是引发该错误的原因。有关原因的更多信息,您可以查看我在 react-leaflet 存储库中提出的问题。 https://github.com/PaulLeCam/react-leaflet/issues/506

jbccollins
2018-12-14