vue-cli
import axios from "axios"
import QS from" qs";
axios.defaults.timeout = 5000;
axios.defaults.headers.post ["Content-Type"] =" application/json";
axios.defaults.baseURL =" http:// address ";
/ / request interceptor
axios.instance.interceptors.request.use (
config = > {
console.log(config)
return config
},
error = > {
return Promise.reject(error);
}
)
/ / respone interceptor
axios.instance.interceptors.response.use (
response = > {
const resp = response.data
if (response.status === 200) {
return resp
}
return resp
},
error = > {
console.log("err" + error)
return Promise.reject(error)
}
)
/ * *
- Encapsulation get method
- @ param url
- @ param data
- @ returns {Promise}
* /
export function fetch (url,params= {}) {
return new Promise ((resolve,reject) = > {
axios.get(url,{
params:params
})
.then(response => {
resolve(response.data);
})
.catch(err => {
reject(err)
})
})
}
/ * *
- encapsulate the post request
- @ param url
- @ param data
- @ returns {Promise}
* /
export function post (url, params) {
return new Promise ((resolve, reject) = > {
axios.post (url, QS.stringify (params))
.then (res = > {
resolve(res.data);
})
.catch (err = > {
reject(err.data)
})
});
}
then
import {post,fetch} from". / api/http.js"
/ / define the global variable
Vue.prototype.$post=post;
Vue.prototype.$fetch=fetch;
wrong Cannot read property "interceptors" of undefined
Why? by the way, what does @ param mean when the boss encapsulates it? thank you