Javascript 未定义React中的Jquery
嗨,我只是想接收ajax请求,但问题是在React中没有定义jquery。React版本为Javascript 未定义React中的Jquery,javascript,jquery,ajax,reactjs,Javascript,Jquery,Ajax,Reactjs,嗨,我只是想接收ajax请求,但问题是在React中没有定义jquery。React版本为14.0 错误消息 我有两个文件: index.js 尝试将jQuery添加到项目中,如 npm i jquery --save 或者如果你使用鲍尔 bower i jquery --save 然后 将“ref”添加到h1标签: <h1 ref="source">Hey there.</h1> 你好。 和 const{source}=this.props更改为const{so
14.0
错误消息
我有两个文件:
index.js
尝试将
jQuery
添加到项目中,如
npm i jquery --save
或者如果你使用鲍尔
bower i jquery --save
然后
将“ref”添加到h1标签:
<h1 ref="source">Hey there.</h1>
你好。
和const{source}=this.props代码>更改为const{source}=this.refs代码>
我只想接收ajax请求,但问题是在React中没有定义jQuery
那就别用了。使用并查看以查看使其运行的其他方法的示例
像这样的
const that = this;
fetch('http://jsonplaceholder.typicode.com/posts')
.then(function(response) { return response.json(); })
.then(function(myJson) {
that.setState({data: myJson}); // for example
});
请注意:如果使用箭头函数,则不需要=此部分的常量。
可能是这样的:
fetch('http://jsonplaceholder.typicode.com/posts')
.then((response) => { return response.json(); })
.then((myJson) => {
this.setState({data: myJson}); // for example
});
不比像这样做容易:
1-在项目中安装jquery:
yarn add jquery
2-导入jquery并开始使用DOM:
import $ from 'jquery';
当项目中没有安装JQuery
时,通常会发生这种情况。
根据包管理器,通过以下命令在项目中安装JQuery
纱线
npm
在此之后,只需在项目文件中导入$
。
从“jquery”导入$
我看不到您在该代码的任何地方定义jquery。为什么您认为它应该工作?为什么还要将jQuery与React?constthat=this一起使用;取('http://jsonplaceholder.typicode.com/posts然后(函数(response){return response.json();})
@mplungjan请回答这个问题,并简单解释一下当已经使用ES6时,fetch
是如何足够的。也许可以提到polyfill可以用于较旧的浏览器。@Emilebergron done。请随意修改答案以反映OPs用途如果您将typescript与react一起使用,则从“jquery”中使用import*as$代码>取而代之。这里有更多信息如果您遇到错误,请尝试运行:1。纱线安装。2.纱线升级。3.纱线添加纱线。它为我解决了所有的问题。我这样做了,但它不起作用。谢谢你的回答,这是真的错过了这个线程!
const that = this;
fetch('http://jsonplaceholder.typicode.com/posts')
.then(function(response) { return response.json(); })
.then(function(myJson) {
that.setState({data: myJson}); // for example
});
fetch('http://jsonplaceholder.typicode.com/posts')
.then((response) => { return response.json(); })
.then((myJson) => {
this.setState({data: myJson}); // for example
});
yarn add jquery
import $ from 'jquery';
yarn add jquery
npm i jquery --save