[英]React: Add jquery needed by another library
I want to use the library Trumbowyg in my react project. 我想使用该库Trumbowyg在我反应过来的项目。 But the problem is the library throws error
jQuery is not defined
. 但是问题是库抛出错误
jQuery is not defined
。
I found info that jquery must be somehow exposed to window as global variable. 我发现jquery必须以某种方式作为全局变量暴露给窗口的信息。 Authors of the library provide example with
Webpack
but I don't have this in my project. 该库的作者提供了
Webpack
示例 ,但我的项目中没有此示例 。
So I've installed jquery through npm install
and tried to import it and set to the window variable like this (it didn't work): 所以我已经通过
npm install
安装了jquery,并尝试将其导入并设置为window变量,如下所示(它不起作用):
import jquery from 'jquery';
window.$ = window.jQuery = jquery;
How can I setup jquery
so that trumbowyg
sees it? 如何设置
jquery
以便trumbowyg
看到它? I've created my project with create-react-app
command if it can help you. 我已经使用
create-react-app
命令创建了我的项目,如果它可以帮助您。
As others commented, you should choose other library which don't use jQuery. 正如其他人所评论的那样,您应该选择其他不使用jQuery的库。 But if you still want to use then you can do like the following:
但是,如果仍然要使用,则可以执行以下操作:
import jquery from 'jquery'
import Trumbowyg from 'react-trumbowyg';
...
componentDidMount() { // hook the jQuery when component is mounted
window.$ = window.jQuery = jquery;
}
render() {
return <Trumbowyg id='react-trumbowyg'/>
}
If this still not working, then require the plugin . 如果仍然无法使用,则需要使用plugin 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.