【发布时间】:2021-07-17 13:15:38
【问题描述】:
我有两个不同版本的代码,但我无法理解为什么第二个版本不能正常工作。 我认为这是一个“上下文”问题,但我不明白。
在这个版本中我得到了回应
// Fist version (it works)
methods: {
async sendDatas() {
await this.$axios({
method: 'post',
url: '/url',
data: {
email: this.email,
},
})
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
},
在这个版本中我无法在 callApi 函数中获取响应数据
sendDatas() {
this.callApi(this.email)
.then((response) => {
// Here "response" is "undefined"
console.log(response)
})
.catch((error) => {
console.log(error)
})
},
async callApi(email) {
await this.$axios({
method: 'post',
url: '/url',
data: {
email,
},
})
.then((response) => {
// Here "response" is ok"
console.log(response)
})
.catch((error) => {
console.log(error)
})
},
async callApi函数返回promise,为什么sendDatas函数中取不到response的内容?
感谢您的帮助:)
【问题讨论】:
标签: javascript api vue.js axios