Re-encapsulation of axios based on promise and how to use it

Create a Request.js file to write the content of the secondary package

//1.首先下载axios模块进行导入
import axios from 'axios'

//2.创建axios对象create
axios.defaults.baseURL = ''

//3.配置请求拦截器
axios.interceptors.request.use(config => {
    
    
    
    return config
})

//4.响应拦截器
axios.interceptors.response.use(response => {
    
    
  
    return response
})

let Server = (params) => {
    
    
    return new Promise((resolve, reject) => {
    
    
        axios({
    
    
            ...params
        }).then(res => {
    
    
            resolve(res)
        }).catch(err => {
    
    
            reject(err)
        })
    })
}
export default Server

Create a new http.js to store the encapsulated interface

//首先导入
 	import  Server  from   '../utils/Request'

	export  const  login = ()  =>Server({
    
      //
    url:'',//请求的路由地址
    method:'', //用来写请求方式
})

Import on the page where the interface needs to be introduced

import {
    
    login} from '@/request/http'

Guess you like

Origin blog.csdn.net/weixin_46300135/article/details/111153190