参考:http://www.axios-js.com/docs/index.html#Browser-Support
fetch.js文件
import axios from 'axios' const instance = axios.create({ baseURL: process.env.VUE_APP_INFO, timeout: 1000, headers: {'Content-Type': 'application/json;charset=UTF-8'}, data:{} }); instance.interceptors.request.use((config)=> { // Do something before request is sent return config; }, function (error) { // Do something with request error return Promise.reject(error); }); // Add a response interceptor instance.interceptors.response.use((res)=> { // Do something with response data let data = res.data; if(+data.error_data){ switch(+data.error_data){ case 30000: console.log("3000") break default: break } } console.log('111') return res }, function (error) { console.log(error) // Do something with response error return Promise.reject(error); }); export default (options) =>{ return new Promise((resolve,reject)=>{ instance(options).then(res=>{ console.log('222') resolve(res.data) }).catch((error)=>{ reject(error) }) }) }
先输出111,再输出222,返回的是promise,所以component文件调用的时候要用async或者then
index.js文件
import fetch from './fetch' export const affProList = (data)=> fetch({ url:'product/affProList', method:'POST', data // data是post,params是get的方式 })
component文件
方法1:
import {affProList} from 'api' let res = affProList({"ksbm": this.secondly.ksbm}); res.then((data)=>{ console.log(data)
})
方法2:
async getSecondlyProduct(){
try(){
let {affProList} = await import('api')
let res = await affProList({"ksbm": this.secondly.ksbm});
}catch(e){
console.log(e)
}
}