详解vue-Resource(与后端数据交互)
作者:艺小晨 时间:2024-06-05 09:15:06
单来说,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
来源:http://www.cnblogs.com/jyichen/p/5660878.html
标签:vue,Resource
0
投稿
猜你喜欢
Python科学计算包numpy用法实例详解
2021-11-12 15:07:20
如何用python实现结构体数组
2023-10-13 19:07:34
简单聊聊Golang中defer预计算参数
2023-07-22 03:55:09
详解python实现简单区块链结构
2023-01-05 22:47:14
详述如何提高MySQL中数据装载效率
2009-10-26 10:27:00
asp连接access数据库表代码实例
2008-04-13 06:18:00
Python闭包装饰器使用方法汇总
2022-12-21 05:47:17
Pycharm使用爬虫时遇到etree红线问题及解决
2021-09-04 19:07:06
什么是XML
2008-09-05 17:21:00
Python使用googletrans报错的解决方法
2021-09-21 01:38:00
Python编程中的异常处理教程
2022-10-16 04:26:23
关注各网站的布局调整
2008-09-23 18:14:00
golang beego框架环境搭建过程
2024-02-13 03:29:39
python中pip的使用和修改下载源的方法
2023-08-04 21:33:41
Python的内存泄漏及gc模块的使用分析
2023-09-17 18:07:30
python模糊图片过滤的方法
2022-07-01 04:37:16
Python自动重新加载模块详解(autoreload module)
2023-09-19 04:36:12
css行高:line-height属性详解
2008-06-24 11:42:00
python获取字符串中的email
2021-04-23 21:23:29
用Python爬取LOL所有的英雄信息以及英雄皮肤的示例代码
2021-07-29 00:54:25