zoukankan      html  css  js  c++  java
  • react 网络请求 axios

    react中通过npm来安装axios扩展

    cnpm i -S axios

    发起请求

    import React, { Component } from 'react'
    
    import axios from 'axios'
    
    export default class App2 extends Component {
    
      state = {
        users: []
      }
    
      // 组件挂载完毕后,执行的方法  只执行一次
      /* componentDidMount() {
        axios.get('http://localhost:3000/data/users.json')
          .then(ret => {
            console.log(ret);
          })
      } */
      async componentDidMount() {
        let { data } = await axios.get('/api/users')
        this.setState(state => ({
          users: data
        }))
      }
    
      render() {
        return (
          <div>
            <ul>
              {
                this.state.users.map(item => (
                  <li key={item.id}>{item.name}</li>
                ))
              }
            </ul>
          </div>
        )
      }
    }
    右侧打赏一下 代码改变世界一块二块也是爱
  • 相关阅读:
    es删除索引
    真人快打11技能键位
    点到线段的距离
    行列快乐值
    按钮变色
    取数求和
    机器分配
    田忌赛马 问题
    雷达
    最小字典序
  • 原文地址:https://www.cnblogs.com/ht955/p/14714250.html
Copyright © 2011-2022 走看看