问题描述

npm上是否有适用于Google地图的软件包?还是我真的应该粘贴这个

Is there any package available on npm for google maps? Or am I really supposed to paste this

<script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY">
</script>

到我的index.html并在每次刷新时下载此js文件?

to my index.html and download this js file on every refresh?

 ReferenceError:未定义google 
ReferenceError: google is not defined

推荐答案

或者我真的应该粘贴这个吗到我的index.html并在每次刷新时下载此js文件?

Or am I really supposed to paste this to my index.html and download this js file on every refresh?

.这是唯一的方法.有一些软件包可以为您动态地执行此操作,但是行为是相同的.

Yes. This is the only way to do so. There are some packages to dynamically do this for you, but the behavior is the same.

 @ google/地图
@google/maps

未定义google 错误.

更新-2020/01/17

我写了 @ googlemaps/js-api-loader 以帮助动态加载脚本并支持诺言.

I wrote @googlemaps/js-api-loader to help load the script dynamically and support promises.

import { Loader } from '@googlemaps/js-api-loader';

const loader = new Loader({
  apiKey: "",
  version: "weekly",
  libraries: []
});

loader
  .load()
  .then(() => {
    new google.maps.Map(div, mapOptions);
  })
  .catch(e => {
    // do something
  });

这篇关于如何通过npm安装Google地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!