【问题标题】:How to display leaflet map properly on NuxtJS如何在 NuxtJS 上正确显示传单地图
【发布时间】:2021-05-23 16:29:04
【问题描述】:

我正在使用 NuxtJS,我使用库 https://vue2-leaflet.netlify.app,但地图没有正确显示。这是我的代码和结果屏幕:

map.vue:

<template>
  <div id="map-wrap" style="height: 100vh">
    <no-ssr>
      <l-map :zoom="13" :center="[55.9464418, 8.1277591]">
        <l-tile-layer
          url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
        ></l-tile-layer>
        <l-marker :lat-lng="[55.9464418, 8.1277591]"></l-marker>
      </l-map>
    </no-ssr>
  </div>
</template>

nuxt-leaflet.js:

import Vue from 'vue';
import { LMap, LTileLayer, LMarker } from 'vue2-leaflet';

Vue.component('l-map', LMap);
Vue.component('l-tile-layer', LTileLayer);
Vue.component('l-marker', LMarker);

nuxt.config.js:

  plugins: [
    {
      src: '~/plugins/nuxt-leaflet',
      mode: 'client',
    },

    {
      src: '~/plugins/vue-my-photos',
      mode: 'client',
    },
  ],

这是结果屏幕:

【问题讨论】:

    标签: javascript vue.js leaflet vue2leaflet


    【解决方案1】:

    最后,我发现我必须包含传单中的 css :

      head() {
        return {
          link: [
            {
              rel: 'stylesheet',
              href: 'https://unpkg.com/leaflet@1.6.0/dist/leaflet.css',
            },
          ],
        }
      },
    

    【讨论】:

      【解决方案2】:

      我也在使用 Vue Leaflet,它运行良好。尝试将 center 属性从数组更改为对象。

      <l-map :zoom="13" :center="{
              lat: -7.280976,
              lng: 112.796844,
            }">
      

      完整代码

          <div style="height: 350px; width: 100%">
            <client-only>
              <l-map
                ref="myMap"
                :zoom="zoom"
                :center="center"
                @update:center="centerUpdated"
                @click="handleClick"
              >
                <l-marker :lat-lng="regionCenter">
                  <l-popup>Lokasi outlet</l-popup>
                </l-marker>
                <l-polyline
                  :lat-lngs="polyline.latlngs"
                  :color="polyline.color"
                ></l-polyline>
                <l-tile-layer :url="url" :attribution="attribution"></l-tile-layer>
              </l-map>
            </client-only>
          </div>
      
      data () {
        return {
            url: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
            attribution:
              '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
            zoom: 15,
            center: {
              lat: -7.280976,
              lng: 112.796844,
            },
            bounds: null,
            regionCenter: [-7.280976, 112.794944],
            address: {
              long: '',
              display: '',
            },
            polyline: {
              color: 'red',
              latlngs: [],
            },
      
        }
      }
      

      【讨论】:

      • 谢谢老兄,但我只是没有包含 CSS 文件。
      猜你喜欢
      • 2019-06-21
      • 2017-03-14
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2019-10-15
      • 2013-12-22
      • 1970-01-01
      相关资源
      最近更新 更多