直接return接口,获取不到接口返回的值
commitNote () {
const data = {
id: this.id,
title: this.title,
folder: this.folder
}
this.loading = true
return api.update(data).then(res => {
this.loading = false
if (res.error_code === 0) {
this.$message.success('更新成功')
}
})
},
handleCommit () {
this.commitNote().then(res => {
console.log(res, 123)
})
}
在handleCommit中,输出的res为undefined,不能直接获得接口返回的数据情况。
commitNote () {
const data = {
id: this.id,
title: this.title,
folder: this.folder
}
this.loading = true
return new Promise((resolve, reject) => {
api.update(data).then(res => {
this.loading = false
if (res.error_code === 0) {
this.$message.success('更新成功')
const result = {
...res,
apiType: 'edit'
}
resolve(result)
}
})
})
},
handleCommit () {
this.commitNote().then(res => {
console.log(res, 456)
})
}
使用new Promise() 并将接口返回值在resolve中返回,在then后即可获取到值