在react的create-react-app中使用OpenLayers来渲染map.Getting跟随错误

时间:2017-10-30 13:25:37

标签: reactjs webpack openlayers openlayers-3 create-react-app

要包含我做过的OpenLayers : npm install ol --save

并实施如下:

    import React, { Component } from 'react';
    import 'ol/ol.css';
    import ol from 'ol';
    import Map from 'ol/map';
    import View from 'ol/view';
    import Tile from 'ol/layer/tile';


    class MapComponent extends Component {

        componentDidMount() {
            var map = new ol.Map({
                view: new ol.View({
                    center: [0, 0],
                    zoom: 1
                }),
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM()
                    })
                ],
                target: 'map'
            });
        }
        render() {

            return (
                <div id="map"></div>
            )
        }
    }

    export default MapComponent;

错误:未捕获的TypeError: WEBPACK_IMPORTED_MODULE_1_ol .a.View不是构造函数

1 个答案:

答案 0 :(得分:0)

您已从ol单独导入View。您应该尝试使用:

view: new View({...

同样适用于上面一行的地图。