【发布时间】:2018-01-30 07:41:46
【问题描述】:
我正在尝试在 ES6 文件上添加回调,但没有找到。
我收到此错误消息:“initMap 不是函数”
我的文件是这样的:
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=<myKey>&callback=initMap"></script>
而我的 js 文件是:
export function initMap()
{
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
fetch('/data/markers.json')
.then(function(response){return response.json()})
.then(plotMarkers);
}
我正在使用browserify和babelify转译js文件
我尝试上下移动,但到目前为止没有运气,唯一可行的方法是直接在 html 上添加 initMap 函数,如本指南所述:
https://developers.google.com/maps/documentation/javascript/adding-a-google-map
实际上我无法找到/理解 ES6 上的函数在哪里运行(范围是)我在 initMap 函数中打印了 this 值,它是未定义的.
【问题讨论】:
-
export function initMap你导入这个导出吗? -
不,我添加导出只是为了测试,但我尝试在另一个 js (main.js) 中导入该函数,它也不起作用。
-
好吧,删除
export,并在(可以肯定)加载 googleapis 之前加载您的 js 文件(您没有显示如何或在何处执行此操作)-哦,等等... browserify ...没有读到那一点 -
我们无法真正可靠地帮助您查看代码的整个布局、包含的内容、包含的内容等...您加载 Google 地图的方式,
initMap()需要是全局定义的函数,在 google 初始化时可用。
标签: javascript google-maps scope callback ecmascript-6