这里封装axios
import axios from 'axios'
import vue from '@/main'
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
})
// axios中间件拦截器
// 窗口弹出异常信息
service.interceptors.response.use(
response => {
return response
},
error => {
if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
vue.$message.error('请求超时:'+error.message)
}else vue.$message.error('error:'+error.message)
//return Promise.reject(error) // 这里return 会传递异常到上一层 不return就不会传递
}
)
export default service
return Promise.reject(error) // 这里return 会传递异常到上一层 不return就不会传递
理论上axios可以简化成
import axios from '@/utils';
axios.post('/comment/addone',
this.comment_info
).then(response=>{
if(response.data.code===0)this.$message.error(response.data.msg)
else {
this.type2_list = response.data.map.type2_list
}
})
理论上所有async都可以.then
this.updateuserinfo()
.then(response=>{
if(response)this.$message.success("删除成功")
else this.obj.addresses = JSON.parse(JSON.stringify(adr_before)) // 删除失败复位
})
// 点修改
async updateuserinfo(){
//if(this.obj.age!=nul
async test() {
const userInfo = await this.getuserinfo();
const historyList = await this.getchat_history_list();
return {userInfo, historyList}; // 返回这两个值的组合
}
this.test().then(response => {
console.log(response); // 这里将输出 {userInfo: ..., historyList: ...}
});