简体   繁体   English

OpenLayers 6 - ES6 项目结构

[英]OpenLayers 6 - ES6 project structure

I'm working on a project using OpenLayers 6 in ES6 with Webpack.我正在使用带有 Webpack 的 ES6 中的 OpenLayers 6 进行项目。
It's my first real ES6 project and I want to make it organized (and a bit modular) but I'm struggling with the use of imports and exports.这是我的第一个真正的 ES6 项目,我想让它有条理(并且有点模块化),但我在使用导入和导出方面遇到了困难。

Currently my structure is:目前我的结构是:

- all.js
- map/
   - index.js
   - gpx.js

The all.js file is the "entry point". all.js文件是“入口点”。

all.js all.js

import 'ol/ol.css';
import map from './map/index';
import { vector as GPXvector } from './map/gpx';

map.addLayer(GPXvector);

map/index.js地图/index.js

import { Map, View } from 'ol';
import { OSM } from 'ol/source';
import { Tile as TileLayer } from 'ol/layer';

const map = new Map({
    layers: [
        new TileLayer({
            source: new OSM()
        })
    ],
    target: 'map',
    view: new View({
        center: [1037749, 5135381],
        zoom: 10
    })
});

export { map as default };

map/gpx.js地图/gpx.js

import { Vector as VectorLayer } from 'ol/layer';
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
import { unByKey } from 'ol/Observable';
import VectorSource from 'ol/source/Vector';
import GPX from 'ol/format/GPX';
import map from './index.js'; // Is that good ??

const style = {
    // [...] Some style here
};

const source = new VectorSource({
    url: 'test.gpx',
    format: new GPX()
});

var onChange = source.on('change', function() {
    if (source.getState() == 'ready') {
        map.getView().fit(source.getExtent()); // Access to "map" from "index.js" HERE
        unByKey(onChange);
    }
});

const vector = new VectorLayer({
    source: source,
    style: function (feature) {
        return style[feature.getGeometry().getType()];
    }
});

export { vector, source };

I want to access to the map instance (initialized in map/index.js ) from the map/gpx.js file (see comment in source code).我想从map/gpx.js文件中访问map实例(在map/index.js中初始化)(参见源代码中的注释)。
But I feel like I am importing map from map/index.js inside all.js , which is importing map/gpx.js which himself also imports map from map/index.js .但我觉得我正在从all.js中的map/index.js导入map ,它正在导入map/gpx.js ,而它自己也从map/index.js导入map
It sounds to me like some kind of "loop" imports where it will be a mess to handle the order of imports for example when I'll get more files in my project.在我看来,这听起来像是某种“循环”导入,其中处理导入顺序会很混乱,例如当我在项目中获得更多文件时。

Also if you have any advice for me to start properly with ES6 it's cool !另外,如果您对我正确开始使用 ES6 有任何建议,那就太酷了!

EDIT 1编辑 1

I changed to something else to see if it allows more granularity.我换了别的东西,看看它是否允许更多的粒度。

all.js all.js

import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');

map/gpx.js地图/gpx.js

import { Vector as VectorLayer } from 'ol/layer';
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
import { unByKey } from 'ol/Observable';
import VectorSource from 'ol/source/Vector';
import GPX from 'ol/format/GPX';

const style = {
    // [...] Some style here
};

const _onSourceChange = function(map, source) {
    if (source.getState() == 'ready') {
        map.getView().fit(source.getExtent());
        unByKey(_onSourceChange);
    }
}

export default class {
    constructor(map, url, fit = true) {
        this.map = map;
        this.url = url;
        this.fit = fit;
        this.loadGPX();
    }

    loadGPX() {
        this.source = new VectorSource({
            url: this.url,
            format: new GPX()
        });

        if (this.fit) {
            this.source.on('change', () => _onSourceChange(this.map, this.source));
        }

        this.vector = new VectorLayer({
            source: this.source,
            style: function(feature) {
                return style[feature.getGeometry().getType()];
            }
        });

        this.map.addLayer(this.vector);
    }
};

I think it's cool because it allows to get multiple GPX vectors on the same map instance.我认为这很酷,因为它允许在同一个 map 实例上获取多个 GPX 向量。
But if I want to do more stuff that interacts with my GPX source or vector I will need to pass the instance everytime instead of just importing the GPX file directly.但是,如果我想做更多与我的 GPX 源或向量交互的东西,我将需要每次都传递实例,而不是直接导入 GPX 文件。

What do you think?你怎么看?

You can use CircularDependencyPlugin for webpack to track such circular dependencies.您可以使用webpackCircularDependencyPlugin来跟踪此类循环依赖关系。
There is no circular dependency in your example, import map from './index.js'; // Is that good??您的示例中没有循环依赖, import map from './index.js'; // Is that good?? import map from './index.js'; // Is that good?? is ok.没关系。

Your es6 code is fine to me, I see one var usage ( var onChange =... ), you should replace that.您的 es6 代码对我来说很好,我看到一个var用法( var onChange =... ),您应该替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM