vue 中使用 async/await 将 axios 异步请求同步化处理

时间:2022-06-10 23:34:38

1. axios 常规用法:

export default {
  name: 'Historys',
  data() {
    return { 
      totalData: 0,  
      tableData: []
    }
  },
  created () {
    this.getHistoryData()
  },
  methods: { 
    handleClick (tab) {
      let data = {
        status: tab.name,
        name: this.formInline.user,
        cid: this.formInline.identity,
        start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
        end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
      }
      this.getHistoryData()
    },
    // 统一处理axios请求
    getHistoryData (data) { 
      axios.get('/api/survey/list/', {
        params: data
      }).then((res) => {
        console.log(res)
        this.tableData = res.data.result
        this.totalData = res.data.count
      }).catch((err) => {
        console.log(err)
        alert('请求出错!')
      })
    }
  }
}

2. 使用 asyns/await 将 axios 异步请求同步化:

2.1 当 axios 请求拿到的数据在不同场景下做相同的处理时:

export default {
  name: 'Historys',
  data() {
    return { 
      totalData: 0,  
      tableData: []
    }
  },
  created () {
    this.getHistoryData()
  },
  methods: { 
    handleClick (tab) {
      let data = {
        status: tab.name,
        name: this.formInline.user,
        cid: this.formInline.identity,
        start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
        end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
      }
      this.getHistoryData()
    },
    // 统一处理axios请求
    async getHistoryData (data) {
      try {
        let res = await axios.get('/api/survey/list/', {
          params: data
        })
        this.tableData = res.data.result
        this.totalData = res.data.count
      } catch (err) {
        console.log(err)
        alert('请求出错!')
      }
    }
  }
}

2.2 当 axios 请求拿到的数据在不同场景下做不同的处理时:

export default {
  name: 'Historys',
  data() {
    return { 
      totalData: 0,  
      tableData: []
    }
  },
  async created () {
    try {
      let res = await this.getHistoryData()
      console.log(res)
      // 等拿到返回数据res后再进行处理
      this.tableData = res.data.result
      this.totalData = res.data.count
    } catch (err) {
      console.log(err)
      alert('请求出错')
    } 
  },
  methods: {  
    async handleClick (tab) {
      let data = {
        status: tab.name,
        name: this.formInline.user,
        cid: this.formInline.identity,
        start_time: this.formInline.dateTime ? this.formInline.dateTime[0] : '',
        end_time: this.formInline.dateTime ? this.formInline.dateTime[1] : ''
      }
      try {
        let res = await this.getHistoryData()
        console.log(res)
        // 等拿到返回数据res后再进行处理
        this.tableData = res.data.result
        this.totalData = res.data.count
      } catch (err) {
        console.log(err)
        alert('请求出错')
      } 
    },
    // 封装axios请求,返回promise, 用于调用getHistoryData函数后作不同处理
    getHistoryData (data) {
      return new Promise((resolve, reject) => {
        axios.get('/api/survey/list/', {
          params: data
        }).then((res) => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}