【问题标题】:Vue 3, leaflet: Map container is already initializedVue 3,传单:地图容器已经初始化
【发布时间】: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 &copy; <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


    【解决方案1】:

    我这样做的方法是将'map'+user.id 推入一个数组。然后使用forEach 用不同的地图进行渲染。我遇到的错误是因为 this.map 总是引用同一个实例。

    因此,脚本中的代码应该是:

    <script>
      export default {
      data() {
        return {
          status: true,
          users: [],
          itemRefs:[], // for saving the unique mapid
          accessToken: "Your mapbox token"
        }
      },
      methods: {
        /**
         * instantiate the id to each user to further use
         */
        instantiateId() {
          this.users.map((user,index) => user.id = index)
        },
        setItemRef(el) {
          if (el) {
            // reference: https://v3.vuejs.org/guide/migration/array-refs.html#migration-strategy
            //console.log(el)
            //console.log(el.id)
            this.itemRefs.push(el.id)
          }
        },
        createMap() {
          this.itemRefs.forEach(element => {
            // console.log(element)
            var mymap = L.map(element).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 &copy; <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(mymap);
          });
        }
      },  
      mounted() {
        this.createMap()
      },
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多