您好,登錄后才能下訂單哦!
這篇文章主要介紹了vue中then后的返回值實例分析的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇vue中then后的返回值實例分析文章都會有所收獲,下面我們一起來看看吧。
Promise 中處理的是異步調(diào)用,異步調(diào)用是非阻塞式的,在調(diào)用的時候并不知道它什么時候結(jié)束,也就不會等到他返回一個有效數(shù)據(jù)之后再進行下一步處理
可以使用 async 和 await來得到我們的返回值
在vue 中的函數(shù)加上async
async del(id){ var that=this var params={ sensorCommonId:id } return DelSensorCommonInfo(params).then(function(res) { return Promise.resolve(res.data.Data); }); },
在我們調(diào)用所在的函數(shù)中也加上 async 在調(diào)用del函數(shù)時
async more(){ var index= await that.del(array[i].SensorCommonId) console.log(index) }
function getSomething() { return "something"; } async function testAsync() { return Promise.resolve("hello async"); } async function test() { const v1 = await getSomething(); const v2 = await testAsync(); console.log(v1, v2); } test();
以上傳文件到阿里云為例:
export function uploadObj({ file }, type) { let name = `路徑名/${Date.parse(new Date()) + file.uid}`; //定義唯一的文件名 const fileName = type == 'excel' ? name + ".xlsx" : name; const ContentType = type == 'excel' ? "text/xml" : "image/jpeg"; new OSS(conf).put(fileName, file, { ContentType: ContentType }).then(({ res, url }) => { if (res && res.status == 200) { this.$message.success("上傳成功"); return url } }).catch(() => { this.$message.error("上傳失敗"); }); }
以上代碼能實現(xiàn)上傳圖片/excel到阿里云服務(wù)器,上傳成功后,阿里云服務(wù)會返回一個URL。此時如果直接return url,那么收到的url是undefined。
export function uploadObj({ file }, type, callback) { let name = `路徑名/${Date.parse(new Date()) + file.uid}`; //定義唯一的文件名 const fileName = type == 'excel' ? name + ".xlsx" : name; const ContentType = type == 'excel' ? "text/xml" : "image/jpeg"; new OSS(conf).put(fileName, file, { ContentType: ContentType }).then(({ res, url }) => { if (res && res.status == 200) { this.$message.success("上傳成功"); callback(url) } }).catch(() => { this.$message.error("上傳失敗"); }); }
this.uploadObj({ file }, "excel", url => this.importData(url));
傳入的第三個參數(shù)是回調(diào)函數(shù),這樣在importData方法中,就可以直接獲取到url啦
關(guān)于“vue中then后的返回值實例分析”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“vue中then后的返回值實例分析”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。