詳解vue-Resource(與后端數(shù)據(jù)交互)
單來說,vue-resource就像jQuery里的$.ajax,用來和后端交互數(shù)據(jù)的??梢苑旁赾reated或者ready里面運行來獲取或者更新數(shù)據(jù)...
vue-resource文檔:https://github.com/vuejs/vue-resource/blob/master/docs/http.md
結(jié)合vue-router
data(){ return{ toplist:[], alllist:[] } }, //vue-router route:{ data({to}){ //并發(fā)請求,利用 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數(shù)組, 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數(shù)組, that.$set('alllist',data.data.knowledgeList) },function(error){ //error }) }
post方式同理
將數(shù)據(jù)綁定到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 在后續(xù)會說到--> <!--字段含義: --> <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:發(fā)布時間--> </div> <div class='fr know-img'> <img v-bind:src=item.coverImage /> </div> <div class='clearfix'></div> </a> </li> </ul>
在vue-validator中做post示例 , 將接口請求地址定義為全局詳見VUEX
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實例
- vue.js前后端數(shù)據(jù)交互之提交數(shù)據(jù)操作詳解
- Vue使用json-server進(jìn)行后端數(shù)據(jù)模擬功能
- vue+vuecli+webpack中使用mockjs模擬后端數(shù)據(jù)的示例
- vue2.0 axios前后端數(shù)據(jù)處理實例代碼
- 詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
- vue+element開發(fā)一個谷歌插件的全過程
- 使用vue-element-admin框架從后端動態(tài)獲取菜單功能的實現(xiàn)
- vue+elementUI組件遞歸實現(xiàn)可折疊動態(tài)渲染多級側(cè)邊欄導(dǎo)航
- Vue Element前端應(yīng)用開發(fā)之獲取后端數(shù)據(jù)
相關(guān)文章
Vue與compressor.js實現(xiàn)高效文件壓縮的方法
本文將介紹基于 Vue 框架和 compressor.js 的上傳時文件壓縮實現(xiàn)方法,通過在上傳過程中對文件進(jìn)行壓縮,減小文件大小,提升上傳速度,為用戶創(chuàng)造更快捷、高效的上傳體驗,感興趣的朋友跟隨小編一起看看吧2024-03-03解決ant Design中Select設(shè)置initialValue時的大坑
這篇文章主要介紹了解決ant Design中Select設(shè)置initialValue時的大坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue數(shù)組中出現(xiàn)__ob__:Observer無法取值問題的解決方法
__ob__: Observer這個屬性其實是Vue監(jiān)控變量產(chǎn)生的,下面這篇文章主要給大家介紹了關(guān)于Vue數(shù)組中出現(xiàn)__ob__:?Observer無法取值問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03