您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“vue-Resource與后端數據交互的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“vue-Resource與后端數據交互的示例分析”這篇文章吧。
單來說,vue-resource就像jQuery里的$.ajax,用來和后端交互數據的。可以放在created或者ready里面運行來獲取或者更新數據...
vue-resource文檔:https://github.com/vuejs/vue-resource/blob/master/docs/http.md
結合vue-router
data(){ return{ toplist:[], alllist:[] } }, //vue-router route:{ data({to}){ //并發請求,利用 Promise return Promise.all([ //簡寫 this.$http.get('http://192.168.30.235:9999/rest/knowledge/list',{'websiteId':2,'pageSize':5,'pageNo':1,'isTop':1}), //this.$http.get('http://192.168.30.235:9999/rest/knowledge/list',{'websiteId':2,'pageSize':20,'pageNo':1,'isTop':0}) //不簡寫 this.$http({ method:'GET', url:'http://192.168.30.235:9999/rest/knowledge/list', data:{'websiteId':2,'pageSize':20,'pageNo':1,'isTop':0}, headers: {"X-Requested-With": "XMLHttpRequest"}, emulateJSON: true }) ]).then(function(data){//es5寫法 return{ toplist:data[0].data.knowledgeList, alllist:data[1].data.knowledgeList } //es6寫法 .then()部分 //.then(([toplist,alllist])=>({toplist,alllist})) },function(error){ //error }) } }
在其他地方使用
ready(){ var that=this; var websiteid = 2, pagesize = 20, pageno =1; that.$http({ method:'GET', url:'http://192.168.30.235:9999/rest/knowledge/list', data:{'websiteId':websiteid,'pageSize':pagesize,'pageNo':pageno,'isTop':0} }).then(function(data){ //賦值給alllist數組, that.$set('alllist',data.data.knowledgeList) }) //簡寫 /*that.$http.get('http://192.168.30.235:9999/knowledge/list',{'websiteId':2,'pageSize':20,'pageNo':1,'isTop':0}).then(function(response){ that.$set('alllist',response.data.knowledgeList) })*/ }
若定義全部變量(在data()中定義),使用$get()獲取
data(){ return{ toplist:[], alllist:[], websiteid:2, pagesize:20, pageno:1 } }, ready(){ var that=this; that.$http({ method:'GET', url:'http://192.168.30.235:9999/rest/knowledge/list', //使用定義的全局變量 用$get()獲取 data:{'websiteId':that.$get('websiteid'),'pageSize':that.$get('pagesize'),'pageNo':that.$get('pageno'),'isTop':0} }).then(function(data){ //賦值給alllist數組, that.$set('alllist',data.data.knowledgeList) },function(error){ //error }) }
post方式同理
將數據綁定到dom上
<ul> <li v-for="item in alllist" v-if="item.istop == false"> <a v-link="{ name: 'getReceiptDetail',params:{knowledgeId: item.id }}"> <div class='fl know-info'> <!-- | limit 和 | timer是filter 在后續會說到--> <!--字段含義: --> <p class='font-normal nomal-height'>{{item.title | limit 30 }}</p> <p class='co9a9a9a' ><span style='margin-right: 1rem;'>{{item.viewTimes}}K</span><span>{{item.publishTime | timer }}</span></p> <!--viewTimes:有多少人查看 , publishTime:發布時間--> </div> <div class='fr know-img'> <img v-bind:src=item.coverImage /> </div> <div class='clearfix'></div> </a> </li> </ul>
在vue-validator中做post示例 , 將接口請求地址定義為全局詳見VUEX
以上是“vue-Resource與后端數據交互的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。