Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript OpenLayers 6-ES6项目结构_Javascript_Webpack_Ecmascript 6_Openlayers - Fatal编程技术网

Javascript OpenLayers 6-ES6项目结构

Javascript OpenLayers 6-ES6项目结构,javascript,webpack,ecmascript-6,openlayers,Javascript,Webpack,Ecmascript 6,Openlayers,我正在使用ES6中的OpenLayers 6和Webpack进行一个项目。 这是我的第一个真正的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 './m

我正在使用ES6中的OpenLayers 6和Webpack进行一个项目。
这是我的第一个真正的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);
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 };
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 };
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');
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/index.js

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

map.addLayer(GPXvector);
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 };
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 };
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');
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.js

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

map.addLayer(GPXvector);
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 };
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 };
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');
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.js
文件访问
map
实例(在
map/index.js
中初始化)。
但是我觉得我正在从
map/index.js
内部
all.js
导入
map/gpx.js
,它自己也从
map/index.js
导入
map
对我来说,这听起来像某种“循环”导入,处理导入顺序会很混乱,例如,当我在项目中获得更多文件时

此外,如果你有任何建议,让我开始正确的ES6它的酷

编辑1 我换了别的东西,看看它是否允许更大的粒度

all.js

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

map.addLayer(GPXvector);
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 };
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 };
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');
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.js

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

map.addLayer(GPXvector);
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 };
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 };
import 'ol/ol.css';
import map from './ntrak/index';
import MyGPX from './ntrak/gpx';

const gpx = new MyGPX(map, 'test.gpx');
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文件

您觉得怎么样?

您可以使用
网页
来跟踪这种循环依赖关系。
您的示例中没有循环依赖项,
import map from./index.js';//好吗???
可以


您的es6代码对我来说很好,我看到一个
var
用法(
var onChange=…
),您应该替换它。

谢谢您的回答,我编辑了我的问题,因为我还尝试导出一个类。我想知道一种或另一种解决方案的缺点。