Promise Vue笔记

这里封装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: ...}
});

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值