国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

vue fetch中的.then()的正確使用方法

瀏覽:2日期:2023-01-26 11:29:44

先看一段代碼:

fetch(’http://localhost:3000/books?id=123456’,{ method:’get’}).then(function(value1){ console.log(value1); return ’hello’;}).then(function(value2){ console.log(value2); return ’HelloWorld’;})/*.then(function(data){ console.log(data); return data.text(); })*/.then(data=>{ console.log(data);})

// 接口app.get(’/books’, (req, res) => { res.send(’傳統(tǒng)的URL傳遞參數(shù)!’ + req.query.id)})

vue fetch中的.then()的正確使用方法

在這段代碼中我們發(fā)現(xiàn),最初傳入的是一個對象,緊接著后一個.then()的傳入?yún)?shù)使用了前一個.then()的返回值,換句話說,就是后一個then使用前一個then的封裝結(jié)果

那么現(xiàn)在去掉注釋:

vue fetch中的.then()的正確使用方法

.then(function(value2){ console.log(value2); return ’HelloWorld’;}).then(function(data){ console.log(data); return data.text(); })text()方法屬于fetch API的一部分,返回一個Promise實例對象,用于獲取后臺返回的數(shù)據(jù)

這段代碼中,傳入的data是上一步封裝的字符串,所以此時用data.text()報錯,除非data為對象

下面演示正確使用方式:

fetch(’http://localhost:3000/books?id=123456’,{ method:’get’}).then(function(data){ console.log(data); console.log(typeof(data)); return data.text();}).then(data=>{ console.log(data); console.log(typeof(data));})

vue fetch中的.then()的正確使用方法

輸出了接口詢問的內(nèi)容,為String類型

到此這篇關(guān)于vue fetch中的.then()的正確使用方法的文章就介紹到這了,更多相關(guān)vue fetch .then()內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 航空| 天津市| 南城县| 遂溪县| 江城| 吉木乃县| 镇远县| 中山市| 嘉荫县| 依兰县| 太白县| 上饶县| 德江县| 土默特右旗| 北票市| 济宁市| 金阳县| 天祝| 泊头市| 龙门县| 松潘县| 三江| 朝阳县| 邯郸市| 镇宁| 蓝田县| 项城市| 洞头县| 西乡县| 富川| 顺昌县| 平湖市| 枣阳市| 东辽县| 隆子县| 塔城市| 馆陶县| 澎湖县| 伊金霍洛旗| 乾安县| 平远县|