谷歌没有在使用create-react-app的反应应用程序中定义 [英] google is not defined in react app using create-react-app

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

问题描述

我使用名为create-react-app的cli创建了一个反应应用程序。看起来Facebook在底下做了很多事情,比如webpack等等。但是,我想它也可能有一些限制。我尝试遵循。



但是我点击play并让应用程序完成运行。我得到谷歌没有定义错误从webpackHotDevClient.js和我的应用程序崩溃。







由于webpack会即时编译文件,因此我认为它无法加载谷歌map via https?



有什么想法?

解决方案

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

  const google = window.google; 

我们强制执行此操作的原因是因为人们通常误解了局部变量,导入模块和全局变量,所以我们希望在使用全局变量时始终在代码中清楚说明。

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

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 .

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

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

Any thoughts?

解决方案

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.

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.

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

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