vue如何使用$http请求数据,需要安装vue-resource

1.安装vue-resource

 npm install vue-resource --save   或者 cnpm install vue-resource --save

 

2.main.js里面引入配置

import VueResource from 'vue-resource'

Vue.use(VueResource);

 

3.使用方法:

mounted(){
    this.init();
  },
methods: {
    init:function () {
     console.log(this);
      var that = this;
      that.$http({           //调用接口
        method:'GET',
        url:'https://www.easy-mock.com/mock/59fdce0e6b54331215b44a24/mcake/goods'  //this指data
      }).then(function(response){  //接口返回数据
        this.imgList=response.data;
        console.log(this.imgList);
      },function(error){
      })
    }
}

 


 

 

vue-从接口请求数据例子:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>获取图片列表</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />       
    </head>
    <body>
        <div id="app">
            <ul>
                <li>
                    <img v-for="imgItem in imgList" v-bind:src="imgItem.img" alt="" width="100%" height="100%"/>
                </li>
            </ul>
        </div>       
    </body>
    <script src="//cdn.bootcss.com/vue/2.1.0/vue.js" type="text/javascript" charset="utf-8"></script>
    <script src="//cdn.bootcss.com/vue-resource/1.0.3/vue-resource.js" type="text/javascript" charset="utf-8"></script>
    <script>
        var demo=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;                        
                    },function(error){
                    })
                }
            }
        })
    </script>
</html>

 

posted @ 2018-02-27 15:27  Shimily  阅读(1018)  评论(0)    收藏  举报