Vue에서 캡슐화, 도메인 간 구성 요청

3295 단어
포장된 요청은 직접 가져가서 사용하세요
import axios from 'axios'
import qs from 'qs'

axios.defaults.timeout = 5000;                        //    
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';        //     
axios.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';        //     
axios.defaults.baseURL = '';   //      

//POST     (       )
axios.interceptors.request.use((config) => {
    //           
    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
    console.log('     ')
    return Promise.reject(error);
});

//PUT     (       )
axios.interceptors.request.use((config) => {
    //           
    if(config.method  === 'put'){
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
    console.log('     ')
    return Promise.reject(error);
});

//      (       )
axios.interceptors.response.use((res) =>{
    //        
    if(!res.data.success){
        return Promise.resolve(res);
    }
    return res;
}, (error) => {
    console.log('    ')
    return Promise.reject(error);
});

//    Promise(  post  )
export function fetchPost(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
//    Promise(  put  )
export function fetchPut(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
//    Promise(  get  )
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
//    Promise(  delete  )
export function fetchDelete(url, param) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    fetchPost,
    fetchGet,
    fetchPut,
    fetchDelete
}

도메인 간 구성
module.exports = {
    //axios   ,  axios    
    // baseUrl: '/',
    publicPath: '/',
    devServer: {
        proxy: {
            '': {
                target: '  IP+  ',
                changeOrigin: true,
                ws: true,
                pathRewrite: {

                }
            }
        }
    }
}

좋은 웹페이지 즐겨찾기