admin管理员组文章数量:1289623
I am trying to use the leaflet map in Vue, but keep getting the error:
Uncaught Error: Map container not found.
This is what my ponent looks like:
<template>
<div id="app" class="container">
Map
<div class="col-md-9">
<div id="mapid"></div>
</div>
</div>
</template>
<style scoped>
#mapid {
height: 800px;
}
</style>
<script>
import L from 'leaflet'
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('/{id}/{z}/{x}/{y}.png?access_token={}', {
attribution: 'Map data © <a href="/">OpenStreetMap</a> contributors, <a href=".0/">CC-BY-SA</a>, Imagery © <a href="/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: ''
}).addTo(mymap);
</script>
I have also added the Leaflet CSS and JavaScript under that in my index.html
head.
I am trying to use the leaflet map in Vue, but keep getting the error:
Uncaught Error: Map container not found.
This is what my ponent looks like:
<template>
<div id="app" class="container">
Map
<div class="col-md-9">
<div id="mapid"></div>
</div>
</div>
</template>
<style scoped>
#mapid {
height: 800px;
}
</style>
<script>
import L from 'leaflet'
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox./v4/{id}/{z}/{x}/{y}.png?access_token={}', {
attribution: 'Map data © <a href="https://www.openstreetmap/">OpenStreetMap</a> contributors, <a href="https://creativemons/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox./">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: ''
}).addTo(mymap);
</script>
I have also added the Leaflet CSS and JavaScript under that in my index.html
head.
-
1
quick fix: put the codes (which creates leaflet map) into the hook=
mounted()
, check this Vue Guide: life cycle hook, because Vue – Sphinx Commented Aug 27, 2018 at 22:08 - attempted but still have the same error. – Kevin Commented Aug 28, 2018 at 2:29
1 Answer
Reset to default 9Wele to SO!
When you try instantiating your Leaflet Map by passing it your Element id (L.map('mapid')
), the problem is that your Vue ponent is not yet attached to your page DOM. Therefore when Leaflet tries to query the latter to find your Element, it cannot find it, hence your error message.
Same thing if you try instantiating in the mounted
lifecycle hook: your Vue ponent instance is created and its fragment HTML structure is built, but still not attached to the page DOM.
But instead of passing your Element id, you can directly pass your Element. Note that you still need to do so in the mounted
hook, to ensure that your ponent instance does have an HTML structure built.
L.map(<HTMLElement> el, <Map options> options?)
Instantiates a map object given an instance of a
<div>
HTML element and optionally an object literal withMap options
.
Then to get your Element, simply leverage Vue $refs
instance property and ref
special attribute, as described in Vue Guide > Accessing Child Component Instances & Child Elements:
[…] sometimes you might still need to directly access a child ponent in JavaScript. To achieve this you can assign a reference ID to the child ponent using the
ref
attribute.
Therefore in your case you would have in your template:
<div id="mapid" ref="mapElement"></div>
And in your ponent script:
import L from 'leaflet'
export default {
mounted() {
var mymap = L.map(this.$refs['mapElement']).setView([51.505, -0.09], 13);
// etc.
},
}
The added advantage of using Vue ref over HTML id is that you can have several Vue ponent instances with their own map, and Vue will reference the appropriate Element to each script.
Whereas with HTML id, if you have several map Elements with same id, Leaflet will get only the first one every time you try to instantiate your map, raising the error that the map is already initialized for this container.
本文标签: javascriptVuejs LeafletMap Container Not FoundStack Overflow
版权声明:本文标题:javascript - Vuejs Leaflet : Map Container Not Found - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741461019a2380038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论