首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能在Vue组件中加载传单

不能在Vue组件中加载传单
EN

Stack Overflow用户
提问于 2017-03-15 17:16:34
回答 4查看 6.9K关注 0票数 1

我试图创建一个传单地图作为一个Vue组件,但我有一些困难的开始。我通过npm安装了传单

我哪里出问题了?Console.log(传单)正在返回一个传单对象,但我在获取要展开和呈现的映射时遇到了困难。

请给我一些指导。

代码语言:javascript
复制
<template>
    <div id="map"></div>
</template>

<script>
    // import leaflet here?
    import Leaflet from 'leaflet';

    export default {
        components: {
            Leaflet
        },

        created() {
            console.log(this);
            console.log(Leaflet);
        },

        ready() {
            this.map = L.map('map').setView([51.959, -8.623], 14);
            L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
                attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            }).addTo(this.map);
        }
    }
</script>

<style>
    #map {
        height: 100%;
        width: 100%;
        margin-top: -24px;
    }

    /* default legend, layer styling from leaflet template */
    .info {
        padding: 6px 8px;
        font: 14px/16px Arial, Helvetica, sans-serif;
        background: white;
        background: rgba(255,255,255,0.8);
        box-shadow: 0 0 15px rgba(0,0,0,0.2);
        border-radius: 5px;
    }
    .info h4 {
        margin: 0 0 5px;
        color: #777;
    }
    .legend {
        text-align: left;
        line-height: 18px;
        color: #555;
    }
    .legend i {
        width: 18px;
        height: 18px;
        float: left;
        margin-right: 8px;
        opacity: 0.7;
    }
</style>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-13 14:47:59

您的代码中有几个问题:

  1. 在Vue中没有ready生命周期挂钩。使用mounted()
  2. 您正在尝试将传单库作为组件传递给Vue,Vue不执行任何操作,因为传单不是Vue组件,并且不需要向Vue注册任何类型的内容。
  3. 没有在Vue组件上声明map数据属性。
  4. 将库导入为Leaflet from 'leaflet'没有问题,但对于您或其他人来说,将传单对象声明为L from 'leaflet'可能更一致。您也可以使用:import { Map } from 'leaflet',但必须适当地初始化地图:this.map = new Map("mapContainer")
  5. 通过使用传单地图类上的remove()方法,适当地防止潜在的内存泄漏和/或清理。一个好的地方是在Vue beforeDestroy生命周期挂钩中。

另外,不要忘记导入传单CSS,例如:import "leaflet/dist/leaflet.css";

代码语言:javascript
复制
<template>
  <div id="mapContainer"></div>
</template>

<script>
import "leaflet/dist/leaflet.css";
import L from "leaflet";

export default {
  name: "LeafletMap",
  data() {
    return {
      map: null
    };
  },
  mounted() {
    this.map = L.map("mapContainer").setView([51.959, -8.623], 12);
    L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
      attribution:
        '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(this.map);
  },
  beforeDestroy() {
    if (this.map) {
      this.map.remove();
    }
  }
};
</script>

<style scoped>
#mapContainer {
  width: 100vw;
  height: 100vh;
}
</style>
票数 7
EN

Stack Overflow用户

发布于 2021-03-10 15:10:45

正确的答案已经由jhickok提供,但对于vue3组合API也很有见解。由于同样的原因,传单地图不能在setup()中实例化。

代码语言:javascript
复制
<template>
  <div id="mapContainer"></div>
</template>

<script>
import { map, tileLayer } from "leaflet";
import { onMounted, onBeforeUnmount } from "vue";

export default {
  name: "Map",
  setup() {
    let container = null;

    onMounted(() => {
      container = map("mapContainer").setView([51.959, -8.623], 12);

      tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
        attribution:
          '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      }).addTo(container);
    });

    onBeforeUnmount(() => {
      container.remove();
    });
  }
};
</script>

<style scoped>
#mapContainer {
  width: 40vw;
  height: 40vh;
}
</style>
票数 0
EN

Stack Overflow用户

发布于 2022-04-24 07:59:25

第一次安装单张和vue2-传单与npm

第二,将代码复制到组件中。

代码语言:javascript
复制
<template>
      <div id="app" v-if="isLoad">
        <l-map style="height: 350px" :zoom="zoom"   :center="center" @click="addMarker"   :max-zoom="maxZoom" :min-zoom="minZoom">
          <l-tile-layer :url="url" :attribution="attribution"></l-tilelayer>
          <l-marker  :lat-lng="markerLatLng" ></l-marker>
        </l-map>
      </div>
</template>

<script>
    import {LMap, LTileLayer, LMarker} from "vue2-leaflet";
    import "leaflet/dist/leaflet.css";
    import {Icon} from "leaflet";
    
    delete Icon.Default.prototype._getIconUrl;
    
    Icon.Default.mergeOptions({
      iconRetinaUrl: require("leaflet/dist/images/marker-icon-2x.png"),
      iconUrl: require("leaflet/dist/images/marker-icon.png"),
      shadowUrl: require("leaflet/dist/images/marker-shadow.png")
    });
    export default {
      components: {
        LMap,
        LTileLayer,
        LMarker,
      },
    
      mounted() {
        this.mapIsReady()
      },
      data() {
        return {
          isLoad: false,
          url: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
          attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
           zoom: 15,
          maxZoom:18,
          minZoom:5,
          center: [35.6892, 51.3890],
          markerLatLng: [35.6892, 51.3890],
        };
      },
      methods: {
        mapIsReady() {
          let self = this
          setTimeout(() => self.isLoad = true, 1500)
        },
        addMarker(event) {
          console.log(event)
          this.markerLatLng = [event.latlng.lat,event.latlng.lng];
        },
    
      },
    };
</script>

此代码为您的地图添加标记、最大缩放和最小缩放。

这个代码使用开放的街道地图,因为它是免费的。如果需要,可以将其更改为api.mapbox.com,但需要api键或accessToken。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42816517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档