美文网首页
React axios 安装与使用

React axios 安装与使用

作者: 张思学 | 来源:发表于2019-11-09 18:25 被阅读0次

    axios 基于Promise的HTTP客户端,用于浏览器和node.js
    github:https://github.com/axios/axios

    安装axios
    npm install axios
    
    使用axios
    import React, {Component, Fragment} from 'react'
    import Http from "axios"; //导入axios
    
    class Index extends Component {
        render(){
          return <div>Hello</div>
        }
        componentDidMount() {
            /* Http = axios
             * Http调用里面的get方法
             * data是参数 get需要key params post不需要直接{id: '007'}
             * .then()成功回调
             * .catch()失败回调
             */
            let data = {
              params:{
                  id: '007'
              }
            }
            Http.get('/api/todolist', data).then(res => {
                console.log(res)
            }).catch(error => {
                console.error(error)
            })
        }
    }
    
    exprot default Index
    

    axios最好的使用方法是自行封装,做拦截器等功能后期会更新

    相关文章

      网友评论

          本文标题:React axios 安装与使用

          本文链接:https://www.haomeiwen.com/subject/ciunbctx.html