'未捕获的ReferenceError:未使用create-react-app定义jQuery' [英] 'Uncaught ReferenceError: jQuery is not defined' with create-react-app
问题描述
使用 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:
- 从'jquery'导入jquery
- 从'jquery'导入jquery,窗口。$ = window.jQuery = jquery;
- 从jQuery导入jQuery 'jquery'
- 从'jquery'导入$
- import'jquery / dist / jquery.js';
- require('。/ node_modules / jquery / dist / jquery.js')
- import jquery from 'jquery'
- import jquery from 'jquery', window.$ = window.jQuery = jquery;
- import jQuery from 'jquery'
- import $ from 'jquery'
- import 'jquery/dist/jquery.js';
- 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屋!