我的 react-google-maps 脚本无法加载

IT技术 reactjs google-maps google-maps-api-3 react-google-maps
2021-05-07 05:56:04

我想呈现组件Searchbox,我在我的应用程序中完全复制了它,但出现错误:

error  Line 44:  'google' is not defined  no-undef   Line 79:  'google' is not defined  no-undef 

我已经加载了我的

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY_KEY_IS_HERE"></script>

进入我的头脑,index.html但无论如何它向我展示了错误。会不会,index.html无法加载我的脚本?

我用 create-react-app 创建了我的应用程序

1个回答

这可能是您的 linter 抱怨未定义的变量。因为“google”是在带有<script>标记的外部加载的脚本中定义的,所以您的 linter 不知道它。尝试为变量添加异常。eslint 中,您可以在访问变量的文件顶部添加这一行: /*global google*/ 但是由于您async<script>标签中有该属性,您的脚本可能仍会在执行地图 API 之前运行。一种解决方案是删除asyncdefer属性。但是如果你真的希望地图 API 异步加载,那么你可以尝试一个额外的库,比如react-async-script-loader. 这是关于类似问题讨论