美文网首页
vue 用async 封装 axios

vue 用async 封装 axios

作者: Isaacyyy | 来源:发表于2020-06-19 09:58 被阅读0次
      import axios from 'axios'
      const qs = require('qs')
      const request= {
      async get (url, data) {
          try {
                let res = await axios.get(url, {params: data})
                res = res.data
          return new Promise((resolve) => {
                if (res.code === 0) {
                  resolve(res)
                } else {
                  resolve(res)
                }
              })
            } catch (err) {
          console.error(err)
        }
      },
      async post (url, data) {
        try {
          let res = await axios.post(url, qs.stringify(data))
          res = res.data
          return new Promise((resolve, reject) => {
         if (res.code === 200 || res.code === 'success') {
              resolve(res)
            } else {
              reject(res)
            }
          })
         } catch (err) {
          console.error(err)
         }
       },
     }
     export { request }
    

    使用

     import { request } from 'common/js/request'
     export default {
        data () {
         return {
            list: [],
          }
        },
        mounted() {
          this.getList()
        },
        methods: {
          async getList () {
            const {data} = await request.get('/test/list')   
            this.list = data
          }
        },
      }

    相关文章

      网友评论

          本文标题:vue 用async 封装 axios

          本文链接:https://www.haomeiwen.com/subject/qmtvxktx.html