[英]OpenLayers 6 - ES6 project structure
我正在使用带有 Webpack 的 ES6 中的 OpenLayers 6 进行项目。
这是我的第一个真正的 ES6 项目,我想让它有条理(并且有点模块化),但我在使用导入和导出方面遇到了困难。
目前我的结构是:
- all.js
- map/
- index.js
- gpx.js
all.js
文件是“入口点”。
all.js
import 'ol/ol.css';
import map from './map/index';
import { vector as GPXvector } from './map/gpx';
map.addLayer(GPXvector);
地图/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 };
地图/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 };
我想从map/gpx.js
文件中访问map
实例(在map/index.js
中初始化)(参见源代码中的注释)。
但我觉得我正在从all.js
中的map/index.js
导入map
,它正在导入map/gpx.js
,而它自己也从map/index.js
导入map
。
在我看来,这听起来像是某种“循环”导入,其中处理导入顺序会很混乱,例如当我在项目中获得更多文件时。
另外,如果您对我正确开始使用 ES6 有任何建议,那就太酷了!
我换了别的东西,看看它是否允许更多的粒度。
all.js
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';
const gpx = new MyGPX(map, 'test.gpx');
地图/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);
}
};
我认为这很酷,因为它允许在同一个 map 实例上获取多个 GPX 向量。
但是,如果我想做更多与我的 GPX 源或向量交互的东西,我将需要每次都传递实例,而不是直接导入 GPX 文件。
你怎么看?
您可以使用webpack
的CircularDependencyPlugin来跟踪此类循环依赖关系。
您的示例中没有循环依赖, import map from './index.js'; // Is that good??
import map from './index.js'; // Is that good??
没关系。
您的 es6 代码对我来说很好,我看到一个var
用法( var onChange =...
),您应该替换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.