例子:Vue 配合 vue-resource 从接口获取数据


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); }) } } })

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM