'未捕获的ReferenceError:未使用create-react-app定义jQuery' [英] 'Uncaught ReferenceError: jQuery is not defined' with create-react-app

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

问题描述

使用 create-react-app 构建React应用程序,以便应用程序是服务器就绪。

Am building a React App using create-react-app so the app is server ready.

导入引导javascript插件(例如'affix.js')时,我收到错误'未捕获的ReferenceError:未定义jQuery',并且页面不再加载。

When importing a bootstrap javascript plugin such as 'affix.js', I get the error 'Uncaught ReferenceError: jQuery is not defined', and the page no longer loads.

我通过index.js文件导入所有模块,因为使用create-react-app它似乎加载了'src'和'link'导入来自index.html文件。

Am importing all the modules through the index.js file since with the create-react-app it dosent seem to load the 'src' and 'link' imports from the index.html file.

导入引导程序插件如下:
import'bootstrap / js / affix.js'

Am importing the bootstrap plugin like so: import 'bootstrap/js/affix.js'

尝试通过以下方式导入jquery,位于index.js文件的顶部但没有一个正常工作:

Have tried importing jquery in the following ways, at the top of the index.js file but none is working:


  1. 从'jquery'导入jquery

  2. 从'jquery'导入jquery,窗口。$ = window.jQuery = jquery;

  3. 从jQuery导入jQuery 'jquery'

  4. 从'jquery'导入$

  5. import'jquery / dist / jquery.js';

  6. require('。/ node_modules / jquery / dist / jquery.js')

  1. import jquery from 'jquery'
  2. import jquery from 'jquery', window.$ = window.jQuery = jquery;
  3. import jQuery from 'jquery'
  4. import $ from 'jquery'
  5. import 'jquery/dist/jquery.js';
  6. require('./node_modules/jquery/dist/jquery.js')

有什么想法吗?

推荐答案

在此创建React App维护者。

Create React App maintainer here.

import $ from 'jquery';

然后使用

$.something()

如果你使用的话应该可以正常工作 jQuery 2.x或更高版本

should work fine if you are using jQuery 2.x or higher.

如果没有,请提出问题。

If it doesn't please file an issue.

在您的示例中,问题可能是您从'jquery'<编写了 import jquery 但使用 jQuery 作为变量名。您应该编写从'jquery'导入jQuery(或任何其他名称,只要您始终如一地使用它)。变量名在JavaScript中区分大小写。

In your examples, the problem might be that you wrote import jquery from 'jquery' but used jQuery as variable name. You should have written import jQuery from 'jquery' (or any other name as long as you use it consistently). Variable names are case sensitive in JavaScript.

这篇关于'未捕获的ReferenceError:未使用create-react-app定义jQuery'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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