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

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

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

瀏覽:6日期: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),最初傳入的是一個(gè)對(duì)象,緊接著后一個(gè).then()的傳入?yún)?shù)使用了前一個(gè).then()的返回值,換句話說,就是后一個(gè)then使用前一個(gè)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的一部分,返回一個(gè)Promise實(shí)例對(duì)象,用于獲取后臺(tái)返回的數(shù)據(jù)

這段代碼中,傳入的data是上一步封裝的字符串,所以此時(shí)用data.text()報(bào)錯(cuò),除非data為對(duì)象

下面演示正確使用方式:

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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 武胜县| 阳朔县| 秦皇岛市| 邵武市| 彭泽县| 恩施市| 永济市| 杨浦区| 竹溪县| 定远县| 田东县| 华亭县| 石棉县| 垦利县| 永福县| 元谋县| 霍山县| 哈巴河县| 白山市| 三明市| 五华县| 平果县| 全州县| 香港 | 壶关县| 古蔺县| 黎平县| 巴林左旗| 康平县| 宁乡县| 郴州市| 新绛县| 建昌县| 万盛区| 天峨县| 临桂县| 纳雍县| 吴堡县| 黄骅市| 洱源县| 万州区|