美文网首页
vue axios 默认异步改为同步

vue axios 默认异步改为同步

作者: jojo1313 | 来源:发表于2020-11-26 17:13 被阅读0次

背景:
调用getvip方法需要先完成gettoken方法,获取token值
目标函数和当前函数都需加上async,await方法即可

methods: {
   async gettoken() {
     console.log(111)
     var api = 'http://192.168.1.9/au?ame=vu&pword=n'
     await axios.get(api).then((response) => {
       this.token = response.data.data.token
     })
   },
   async getvip() {
     await this.gettoken() // 先获取token值
     this.listLoading = true
     var api = `http://192.168.1.9/history?mode=sreach&token=`
     api = api + this.token
     axios.post(api, this.postdata).then((response) => {
       console.log(response.data.data)
       this.data = response.data.data
       this.listLoading = false
       return
     }, (err) => {
       console.log(err)
     })
   },
}

相关文章

网友评论

      本文标题:vue axios 默认异步改为同步

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