'$' 未定义 no-undef reactjs ajaxcall

Pro*_*ova 1 javascript reactjs

我试图根据这个答案以表格格式显示 json 数据。每次我运行这个,它都会显示这个错误Line 15: '$' is not defined no-undef。我在第 15 行开始 ajax 调用。我尝试 const $ = window.$;在代码顶部添加,但这并不能解决任何问题。

class App extends React.Component {
    constructor(){
      super() 
        this.state = {
          data: []
        }
      
    }
    componentDidMount() {
      $.ajax({
         url: "http://www.mocky.io/v2/5c3f2a6c3500004d00ec3789",
         type: "GET",
         dataType: 'json',
         ContentType: 'application/json',
         success: function(data) {
           
           this.setState({data: data});
         }.bind(this),
         error: function(jqXHR) {
           console.log(jqXHR);
         }.bind(this)
      })
    }
    render() {
      
          
      return (
        <table>
        <tbody>{this.state.data.map(function(item, key) {
               
                 return (
                    <tr key = {key}>
                        <td>{item.name}</td>
                        <td>{item.post}</td>
                        <td>{item.country}</td>
                        <td>{item.contact}</td>
                    </tr>
                  )
               
               })}</tbody>
         </table>
      )
    }
  }
  


ReactDOM.render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

对错误有帮助吗?

Uda*_*sun 11

使用create-react-app时,首先需要像这样安装Jquery包。

对于苹果机

sudo npm install jquery --save 
Run Code Online (Sandbox Code Playgroud)

对于Windows

npm install jquery --save
Run Code Online (Sandbox Code Playgroud)

现在在 JS 文件中你可以使用 jquery

import $ from 'jquery';
Run Code Online (Sandbox Code Playgroud)