vue-resource 是 vue 的一个与服务器端通信的 HTTP 插件,用来从服务器端请求数据。
结合例子——图片列表来写一下 Vue获取接口数据。
html :
<div id="app">
<ul>
<li>
<img v-for="imgItem in imgList" v-bind:src="imgItem.img" alt="" width="100%" height="100%"/>
</li>
</ul>
</div>
vue :
var vm = new Vue({ el:'#app', data: { imgList:[], getImgUrl: '' //这里写接口地址
}, created: function(){ this.getImg() //定义方法
}, methods: { getImg: function(){ var that = this; that.$http({ //调用接口
method:'GET', url:this.getImgUrl //this指data
}).then(function(response){ //接口返回的数据
this.imgList=response.data; // promise的then成功之后,将response返回的数据data,保存到imgList数组里
},function(error){ console.log(error); }) } } })