本文小編為大家詳細介紹“vue中如何利用resource發(fā)送請求”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“vue中如何利用resource發(fā)送請求”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)博樂免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
vue resource發(fā)送請求,代碼如下所示;
<!DOCTYPE html> <html> <head> <title>vue-resource</title> <meta charset="utf-8"> </head> <body> <div id="app"> <input type="button" value="get請求" @click="getInfo"> <input type="button" value="post請求" @click="postInfo"> </div> </body> <!-- 基于vue-resource實現(xiàn)get post請求 也可利用axios第三方包實現(xiàn)--> <script src="https://cdn.staticfile.org/vue/2.6.10/vue.js"></script> <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.js"></script> <script> // vue-promise是基于vue的,所以引入CDN庫時需要注意順序,應(yīng)先引入vue let vm = new Vue({ el: "#app", data:{ }, methods:{//vue-resource發(fā)送請求是基于promise的 getInfo(){ this.$http.get('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/result/cart.json') .then(res=>{ console.log(res); }) }, postInfo(){ this.$http.post('https://www.easy-mock.com/mock/5d537a1cf651bc6ff265fb77/example/upload',{},{ emulateJSON:true//設(shè)置這個參數(shù)相當于application/x-www-form-urlencoded,由于手動提交請求沒有默認表單格式,需要設(shè)置發(fā)送的數(shù)據(jù)格式 }) .then(res=>{ console.log(res.body); }) }, } }); </script> </html>
知識點擴展;vue-resource請求的幾種方式
安裝import VueResource from 'vue-resource'
Vue.use(VueResource) get //獲取 getdesignatepl({commit,rootState}){ // console.log("ppppp") let designate_list = `schedule-plan-configuration`; Vue.http.get(api.get(designate_list),{headers:rootState.headers}).then(function(response){ let pup=response.data.data; // console.log(response) },function(response){ // console.log("pl") var tipsObj = { title : "數(shù)據(jù)錯誤", flag: true, type: 4, btnGroup:['ook'] } commit('popup',tipsObj,{root:true}); }) } post//新增 setAdd({commit,rootState,state,dispatch,context},mss){ Vue.http.post(api.save('schedule-plan-configuration'),mss,{headers:rootState.headers}).then(function(response){ dispatch("getdesignatepl") commit('popup',titleObj,{root:true}); },function(response){ if(response.status == 422){ commit('popup',tipsObj,{root:true}); } }) }, //刪除 BrDelete(context,obj){ let br_delete_put_data = `schedule-plan-configuration/${obj.id}` Vue.http.delete(api.get(br_delete_put_data),{headers:context.rootState.headers}).then(function(response){ context.dispatch("getdesignatepl"); },function(response){ // console.log(response); if(response.status == 401 || response.status == 500){ var tipsObj = { status: response.status, flag: true, type: 4, btnGroup:['ook'] } context.commit('popup',tipsObj,{root:true}); return; } }) }, // 跟新 getEntrySales({commit,rootState,dispatch,context},obj){ // console.log(obj.id) Vue.http.put(api.update(`schedule-plan-configuration/${obj.id}`),obj, {headers:rootState.headers}).then(function(response){ dispatch("getdesignatepl"); },function(response){ }) },
讀到這里,這篇“vue中如何利用resource發(fā)送請求”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:vue中如何利用resource發(fā)送請求
網(wǎng)站地址:http://chinadenli.net/article18/giscgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、App開發(fā)、Google、ChatGPT、微信公眾號、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)