vue 中使用 async/await 將 axios 異步請求同步化處理


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)
        })
      })
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM