google 未在使用 create-react-app 的 react 应用程序中定义 [英] google is not defined in react app using create-react-app

查看:23
本文介绍了google 未在使用 create-react-app 的 react 应用程序中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用名为 create-react-app 的 cli 创建了一个 React 应用程序.看起来 Facebook 在底层做了很多事情,比如 webpack 等.但是,我猜它可能也有一些限制.我尝试按照 .

I create a react app using the cli called create-react-app. Look like Facebook did lots of things underneath, such as webpack etc. However, I guess it may also has some limitations. I try to follow .

然后我单击播放并让应用程序完成运行.我从 webpackHotDevClient.js 得到 google is not defined 错误,我的应用程序崩溃了.

But then I click play and let application finishes running. I got google is not defined error from webpackHotDevClient.js and my application crashes.

由于 webpack 即时编译文件,我认为通过 https 加载谷歌地图有问题吗?

Since webpack compile the files on the fly, I assume it has trouble to load google map via https?

有什么想法吗?

推荐答案

用户指南,您需要从 window 显式读取任何全局变量.把它放在文件的顶部,它会起作用:

As mentioned in the user guide, you need to explicitly read any global variables from window. Put this at the top of the file and it will work:

const google = window.google;

我们强制这样做的原因是因为人们通常会误解局部变量、导入模块和全局变量之间的区别,因此我们希望在使用全局变量时始终在代码中明确说明.

The reason we enforce this is because people commonly misunderstand the difference between local variables, imported modules, and global variables, and so we want to always make it clear in the code when you use a global variable.

顺便说一下,这与 Webpack 或 HTTPS 无关.您看到这一点是因为我们使用了禁止未知全局变量的 linting 规则.

By the way, this is not related to Webpack or HTTPS. You see this because we use a linting rule that forbids unknown global variables.

这篇关于google 未在使用 create-react-app 的 react 应用程序中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆