【问题标题】:loading google map from another component with vue router使用 vue 路由器从另一个组件加载谷歌地图
【发布时间】:2020-10-05 13:36:29
【问题描述】:

我有一个关于谷歌地图加载的问题。事情在#App组件中阅读谷歌地图 来自<router-view> 的组件,但这样会给出错误:"ReferenceError: google is not defined"。首先我以为我在initMap() 中遗漏了一些东西,但没有,因为如果我尝试将地图方法粘贴到app.vue 中,那么谷歌地图运行没有问题。

顺便说一句,我的 api 在 public/index.html

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_API&callback=initMap"></script>

ma​​p.vue 组件:

<template>
    <div id="map" class="map"></div>
</template>
<script>
export default {
    mounted(){
        this.initMap();
    },
    methods: {
        initMap(){
            let options = {
                center: {lat: 34.693725, lng: 135.502254},
                zoom: 15,
                mapTypeId: "roadmap"
            };

            let map = new google.maps.Map(document.getElementById("map"), options);

        }
    }
}
</script>

那么您认为问题是什么以及如何使用 &lt;router-view&gt; 映射组件工作

【问题讨论】:

标签: google-maps vue.js vue-router


【解决方案1】:

Vue Cookbook 有一个example for loading google maps,它使用google-maps-api-loader 将加载过程包装成一个promise。 加载器的承诺是 awaited 在消费组件的 mount 钩子中。

这是codesandbox demonstrating the approach

【讨论】:

  • 为什么这可能是合适的,最好在你的答案中包含一些代码和进一步的解释,而不是仅仅将链接粘贴到可能随时停止工作的外部页面。
猜你喜欢
  • 2019-06-26
  • 1970-01-01
  • 2018-10-11
  • 2020-03-23
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多