簡體   English   中英

Vue.js單文件組件中的Mapbox-gl(Quasar-Framework)

[英]Mapbox-gl in a Vue.js single file component (Quasar-Framework)

我想在Quasar Framework(Vue)單文件組件中實現Mapbox-gl-js Map,但是我沒有讓它工作。 我在使用Vue的Googlemaps上找到了一些代碼,在Mapbox上找到了一些帶有React的東西,並嘗試將它從中拉出來。 使用下面的地圖初始化參數,我可以在index.html(使用mapzen圖塊)中顯示正常的地圖,但是希望它在組件中。

我嘗試按照[ https://laracasts.com/discuss/channels/vue/google-maps-and-vue-js](link url)進行調整,然后針對Mapbox進行調整: proj / src / components / maplayout.vue :

<template>
    <quasar-layout>
      <h3>Map</h3>
      <div id='map'></div>
    </quasar-layout>
  </template>

  <script>
  import mapboxgl from '../app'

  export default {
    data () {
      return {}
    },
    create () {
      this.createMap()
    },
    methods: {
      createMap: function () {
        mapboxgl.accessToken = '{{yourmapboxaccestokenkey}}'
        var simple = {
          'version': 8,
          'sources': {
            'osm': {
              'type': 'vector',
              'tiles': ['https://vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt?api_key=vector-tiles-{{yourmapzenapikey}}']
            }
          },
          'layers': [{
            'id': 'background',
            'type': 'background',
            'paint': {
              'background-color': '#adddd2'
            }
          }, {
            'id': 'majorroad',
            'source': 'osm',
            'source-layer': 'roads',
            'type': 'line'
          }, {
            'id': 'buildings',
            'type': 'fill',
            'source': 'osm',
            'source-layer': 'buildings'
          }]
        }

        // initialize the map
        this.map = new mapboxgl.Map({
          container: 'map',
          style: simple,
          center: [-1.83, -78.183],
          zoom: 5.5
        })
      }
    }
  }
  </script>

  <style>
  </style>

順便說一句,對於帶有webpack的mapbox,您需要某些加載器,請參閱:[ https://mikewilliamson.wordpress.com/2016/02/24/using-mapbox-gl-and-webpack-together/ ](link url)但是因為我之前讓Mapbox使用Webpack(沒有vue),我想我沒那么好。 實際上我沒有在瀏覽器控制台中出現任何錯誤(但顯然沒有地圖出現)。

在app.js文件中,我不知道如何處理建議(也許沒有必要,因為googlemaps需要回調,dunno關於mapbox / mapzen?!):

var App = window.App = new Vue ({
//code
})

與Quasar一樣,初始化完成如下:

Quasar.start(() => {
  Router.start(Vue.extend({}), '#quasar-app')
})

我真的沒有...

歡迎任何有關如何使這項工作的建議!

我離得很近。 這實際上有效:

<template>
  <quasar-layout>
  <h3>Map</h3>
  <div id='map'></div>
  </quasar-layout>
</template>

<script>
import mapboxgl from 'mapbox-gl'
console.dir(mapboxgl)

export default {
  data () {
    return {}
  },
  ready () {
    this.createMap()
  },
  methods: {
    createMap: function () {
      mapboxgl.accessToken = '{{yourmapboxaccestokenkey}}'
      var simple = {
        'version': 8,
        'sources': {
          'osm': {
            'type': 'vector',
            'tiles': ['https://vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt?api_key=vector-tiles-{{yourmapzenapikey}}']
          }
        },
        'layers': [{
          'id': 'background',
          'type': 'background',
          'paint': {
            'background-color': '#bbccd2'
          }
        },
          {
            'id': 'majorroad',
            'source': 'osm',
            'source-layer': 'roads',
            'type': 'line'
          },
          {
            'id': 'buildings',
            'type': 'fill',
            'source': 'osm',
            'source-layer': 'buildings'
          }]
      }

      // init the map
      this.map = new mapboxgl.Map({
        container: 'map',
        style: simple,
        minzoom: 1.3,
        center: [-74.0073, 40.7124], // Manhattan
        zoom: 16
      })

      this.map.addControl(new mapboxgl.Navigation())
    }
  }
}
</script>

<style>
</style>

我沒有改變我的Vue初始化。

<template>
  <div class="hello">
    <div id='map'></div>
  </div>
</template>

<script>
import mapboxgl from 'mapbox-gl';

require('../node_modules/mapbox-gl/dist/mapbox-gl.css');

export default {
  name: 'HelloWorld',
  data() {
    return {
      apiKey: {{ your api key }},
    };
  },
  mounted() {
    this.createMap();
  },
  methods: {
    createMap() {
      mapboxgl.accessToken = this.apiKey;
      // init the map
      this.map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v9',
        minzoom: 1.3,
        center: [-74.0073, 40.7124], // Manhattan
        zoom: 16,
      });

      this.map.addControl(new mapboxgl.Navigation());
    },
  },
};
</script>

我想這可能會有所幫助!

我注意到的第一件事是:在將DOM注入文檔之前,您正在初始化地圖。 而不是'created()'方法使用'ready()'。

Quasar基於Vue2版本。 因此不推薦使用ready方法,而不是使用掛載方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM