【问题标题】:Uncaught TypeError: _arc2.default.GreatCircle is not a constructor未捕获的类型错误:_arc2.default.GreatCircle 不是构造函数
【发布时间】:2017-08-02 18:48:09
【问题描述】:

尝试为 react-leaflet 创建插件。我收到此错误,而 我回来了

L.Polyline.Arc(position.from, position.to, option)

这是我的组件

import React, { PropTypes } from 'react';
import { Path } from 'react-leaflet';
import L from 'leaflet'
import { Arc } from './leaflet.arc';


export default class ArcLine extends Path {
  static propTypes = {
    children: PropTypes.oneOfType([
      PropTypes.arrayOf(PropTypes.node),
      PropTypes.node,
    ]),
    option:PropTypes.object,
    position: PropTypes.object.isRequired
  };

  createLeafletElement (props) {
    const { position, option, ...options } = props
    return L.Polyline.Arc(position.from, position.to, option)
  }

  updateLeafletElement (fromProps, toProps) {
    if (toProps.position !== fromProps.position) {
      this.leafletElement._createLatLngs(line, toProps.position.from)
    }
    this.setStyleIfChanged(fromProps, toProps)
  }
}

./leaflet.arc 是

import arc from 'arc';
/**
 * Transform L.LatLng to {x, y} object
 * @param {L.LatLng} latlng
 * @returns {{x: {number}, y: {number}}}
 * @private
 */
const _latLngToXY = latlng => ({
    x: latlng.lng,
    y: latlng.lat
});

/**
 * Create array of L.LatLng objects from line produced by arc.js
 * @param {object} line
 * @param {L.LatLng} from
 * @private
 * @returns {Array}
 */
function _createLatLngs(line, from) {
    if (line.geometries[0] && line.geometries[0].coords[0]) {
        /**
         * stores how many times arc is broken over 180 longitude
         * @type {number}
         */
        let wrap = from.lng - line.geometries[0].coords[0][0] - 360;

        return line.geometries
            .map(subLine => {
                wrap += 360;
                return subLine.coords.map(point => L.latLng([point[1], point[0] + wrap]));
            })
            .reduce((all, latlngs) => all.concat(latlngs));
    } else {
        return [];
    }
}

if (!L) {
    throw "Leaflet.js not included";
}  else if (!arc && !arc.GreatCircle) {
    throw "arc.js not included";
} else {
    L.Polyline.Arc = function (from, to, options) {
        from = L.latLng(from);
        to = L.latLng(to);
debugger

        var vertices = 10;
        var arcOptions = {};
        if (options) {
            if (options.offset) {
                arcOptions.offset = options.offset;
                delete options.offset;
            }
            if (options.vertices) {
                vertices = options.vertices;
                delete options.vertices;
            }
        }

        var generator = new arc.GreatCircle({x: from.lng, y: from.lat}, {x: to.lng, y: to.lat});

        var line = generator.Arc(vertices, arcOptions);
        var latLngs = [];

        var wrap = 0; // counts how many times arc is broken over 180 degree
        if (line.geometries[0] && line.geometries[0].coords[0])
            wrap = from.lng - line.geometries[0].coords[0][0];

        line.geometries.forEach(function(line) {
            line.coords.forEach(function (point) {
                latLngs.push(L.latLng(
                    [point[1], point[0] + wrap]
                ));
            });
            wrap += 360;
        });
        line.geometries[0].coords
        return L.polyline(latLngs, options);
    };
}

为什么会出现这个错误,有什么建议吗?

【问题讨论】:

    标签: leaflet automatic-ref-counting react-leaflet


    【解决方案1】:

    在这一行:

    import { Arc } from './leaflet.arc'
    

    ...您正在尝试从leaflet.arc 文件中导入 Arc 变量。但是,在您的 leaflet.arc 文件中,您似乎没有导出任何内容,只是将 Arc 函数附加到 Leaflet 的 Polyline 对象。此外,在组件的代码中,您甚至没有使用您尝试导入的 Arc 变量。因此,代码失败是因为试图从leaflet.arc 的未定义导出中读取属性。看来您应该只导入文件而不尝试导入任何对象。

    TLDR:

    替换这一行:

    import { Arc } from './leaflet.arc'
    

    用这个:

    import './leaflet.arc'
    

    【讨论】:

    • 我也试过了,但同样的错误。使用调试器,我的代码正在工作,正如我所见,数据已传递,但当 var generator = new arc.GreatCircle({x: from.lng, y: from.lat}, {x: to.lng, y: to .lat});
    • 尝试在您的 leaflet.arc 文件中将此行:} else if (!arc && !arc.GreatCircle) { 更改为:} else if (!arc || !arc.GreatCircle) {
    • 表示未包含未捕获的 arc.js,并且使用 console.log(arc) 我得到空对象。但我已经包含了弧。
    • 你的 package.json 中包含 arc 并安装了吗?
    • 是的,我使用 npm install --save arc,在 packge json 我有依赖 "arc": "^0.1.0"
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多