vue中怎么利用resource發(fā)送請(qǐng)求-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)vue中怎么利用resource發(fā)送請(qǐng)求,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供師宗網(wǎng)站建設(shè)、師宗做網(wǎng)站、師宗網(wǎng)站設(shè)計(jì)、師宗網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、師宗企業(yè)網(wǎng)站模板建站服務(wù),10多年師宗做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

vue resource發(fā)送請(qǐng)求,代碼如下所示;

<!DOCTYPE html>
<html>
<head>
  <title>vue-resource</title>
  <meta charset="utf-8">
</head>

<body>
  <div id="app">
    <input type="button" value="get請(qǐng)求" @click="getInfo">
    <input type="button" value="post請(qǐng)求" @click="postInfo">
  </div>
</body>
<!-- 基于vue-resource實(shí)現(xiàn)get post請(qǐng)求 也可利用axios第三方包實(shí)現(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庫時(shí)需要注意順序,應(yīng)先引入vue
  let vm = new Vue({
    el: "#app",
    data:{
    },
    methods:{//vue-resource發(fā)送請(qǐng)求是基于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è)置這個(gè)參數(shù)相當(dāng)于application/x-www-form-urlencoded,由于手動(dòng)提交請(qǐng)求沒有默認(rèn)表單格式,需要設(shè)置發(fā)送的數(shù)據(jù)格式
        })
        .then(res=>{
          console.log(res.body);
        })
      },
      }
  });
</script>
</html>

知識(shí)點(diǎn)擴(kuò)展;vue-resource請(qǐng)求的幾種方式

vue中怎么利用resource發(fā)送請(qǐng)求

安裝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ù)錯(cuò)誤",
      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ā)送請(qǐng)求,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享名稱:vue中怎么利用resource發(fā)送請(qǐng)求-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article26/dshscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站策劃、網(wǎng)站維護(hù)搜索引擎優(yōu)化、靜態(tài)網(wǎng)站企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)