Axios encapsulation
1. Create an init.js file in the vue framework
import axios from "axios"
import store from './store'
console.log(store)
letThe req = axios. Create ({}) the req. Interceptors. Request. Use (/ / config representative are some of the information you requested config = > {store. State. The done =true
console.log(store.state.done)
return config
},
error=>{
returnPromise.error(error) } ); / / response to intercept the req. Interceptors. Response. Use (response = > {if(response.status==200){
setTimeout(()=>{
store.state.done=false}, 2000).return Promise.resolve(response)
}else{
return Promise.reject(response)
}
},
error=>{
if(error.response.status){
return Promise.reject(error.response)
}
}
)
exportdefault req; Copy the codeCopy the code
2. Create an api.js file
import req from "./init"
// console.log(req)
exportDefault {// External interfacegetlist() {return req({
type:"get",
url:"http://mengxuegu.com:7300/mock/5ed8a7f631561b0233dde2de/api/api"})}, // local custom interface fetchgetlogin() {return req({
type:"get",
url:"/login.json"})}} copy the codeCopy the code
3, which component needs to be introduced and called in, where
created(){
text.getlist().then(res=>{
console.log(res.data.data.data)
})
text.getlogin().then(res=>{
console.log(res)
})
},
Copy the code