【发布时间】:2021-07-16 01:16:47
【问题描述】:
我意识到 issue 与经典 javascript 相同。但是,我的目标是使用v-for 和"'map'+user.id"、:ref 为每个引导卡动态创建经度、纬度传单地图。我想知道如何在 vue 3 中解决这个问题。
从控制台,我确认每个块可以有不同的 id 但相同的类。
我在启动多个实例时发现了这篇带有 vuejs2 ref 的帖子。好久不见,现在docs 说明了在方法中使用ref 的正确方法。
这里是CodeSandBox。 (不过,在 localhost 中渲染)
以下是相关代码:
<template>
<div class="container">
<div class="row">
<div class="col-md-4 mb-3" v-for="user in users" :key="user.id">
<div :id="'map'+user.id" :ref="setItemRef" class="map"></div>
</div>
</div>
</template>
<script>
import users from '@/data/data.js'
import L from 'leaflet'
export default {
data() {
return {
status: true,
users: [],
map: null,
accessToken: "Token shown in the CodeSandBox"
}
},
methods: {
setItemRef(el) {
if (el) {
// reference: https://v3.vuejs.org/guide/migration/array-refs.html#migration-strategy
// this.itemRefs.push(el)
console.log(el)
this.map = L.map(el).setView([51.505, -0.09], 13);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: this.accessToken
}).addTo(this.map);
// this.map.off()
// this.map.remove()
//console.log(this.map)
}
},
},
</script>
<style>
.map {
width: 400px;
height: 400px;
}
</style>
【问题讨论】:
标签: javascript leaflet vuejs3