简体   繁体   English

未捕获的引用错误:未定义$(Reactjs)

[英]Uncaught Reference Error: $ is not defined (Reactjs)

what could be the reason of this error? 该错误的原因是什么?

this is my code 这是我的代码

componentDidMount() {
    $.get("/api/v1/schools", function(result) {
        this.setState({
            dataSchools: result,
        });
    }.bind(this));
}

Note: I am not recommending use of jquery only for ajax calls in ReactJs application. 注意:我不建议仅在ReactJs应用程序中对ajax调用使用jquery。 You can use libraries like superagent or needle etc. 您可以使用诸如superagentneedle等库。

Coming to your error: that jquery is not identified inside your JSX code. 出现错误:在您的JSX代码中未标识该jquery。 The reason is JS script inclusion precedence. 原因是JS脚本包含优先级。 Make sure you are including the jQuery library ahead of your bundle Js(the Js generated using your buldlers like webpack or CommonJs). 确保在捆绑包Js(使用像webpack或CommonJs之类的buldlers生成的Js)之前包括jQuery库。 To be in safer side keep the script to include the jQuery library before all other libraries. 为了安全起见,请保留脚本以在所有其他库之前包含jQuery库。 Because there are many other library which depend upon jQuery to function properly. 因为还有许多其他依赖jQuery才能正常运行的库。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM