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

您的位置:首頁技術文章
文章詳情頁

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(’傳統的URL傳遞參數!’ + req.query.id)})

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

在這段代碼中我們發現,最初傳入的是一個對象,緊接著后一個.then()的傳入參數使用了前一個.then()的返回值,換句話說,就是后一個then使用前一個then的封裝結果

那么現在去掉注釋:

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

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

這段代碼中,傳入的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()的正確使用方法

輸出了接口詢問的內容,為String類型

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

標簽: Vue
相關文章:
主站蜘蛛池模板: 长垣县| 西宁市| 会昌县| 浏阳市| 安义县| 垣曲县| 客服| 凤阳县| 孝昌县| 资溪县| 榆社县| 鄱阳县| 乳源| 建水县| 庆安县| 沧州市| 伊吾县| 平阴县| 二连浩特市| 鸡东县| 济南市| 博白县| 东莞市| 衡东县| 河北省| 芒康县| 石景山区| 旅游| 汪清县| 无为县| 珲春市| 宁都县| 峡江县| 曲沃县| 石狮市| 阿拉善盟| 定结县| 法库县| 托里县| 紫金县| 乌拉特前旗|