在我的应用程序中,我有一条路线map.svelte
这有两个子组件:Map.svelte
和LayerSelector.svelte
Map.svelte
创建一个 OpenLayersmap
实例。LayerSelector.svelte
需要访问这个实例。
我正在声明map
实例map.svelte
并将其绑定到两个组件,但似乎无法访问它LayerSelector.svelte
map.svelte:
<script>
import Map from '../components/Map.svelte';
import MapLayerSelectorDialog from '../components/LayerSelector.svelte';
let map;
</script>
<div class="map" id="map">
<Map organisation={organisation} meta={meta}
showBackControl="true" showEditControl="true" showSearchControl="true"
bind:map={map}
on:mapready={initLayerSelector(map)}
on:map-back={back=goBack}
on:featureselected={featureSelected}
on:togglelayerselector={toggleLayerSelector}
on:togglemapsearch={toggleMapSearch}
on:togglemapeditcontrol={toggleMapEdit}
/>
</div>
<MapLayerSelectorDialog bind:showDialog={showLayerSelectorDialog}
bind:meta = {meta}
bind:map={map}>
</MapLayerSelectorDialog>
Map.svelte:
export let map = null;
...
onMount(async () => {
map = await initMap ('map', organisation, meta, startCoords);
});
...
initMap.js
export async function initMap (div, organisation, meta, startCoords) {
var map = new Map({
target: div, //'map',
view: new View({
center: fromLonLat ([1330601.87, 7916443.12]),
zoom: 12,
}),
});
addLayers (map, meta);
});
层选择器.svelte:
<script>
export let map
</script>
{#each map.getLayers() as layer}
<p>{layer.get('name')}</p>
{/each}
我可能哪里出错了?我是否需要将 OpenLayersmap
实例放在可写存储中?