vue-resource

vue-resource提供了两种方法进行ajax请求,一种是在vue组件内部使用

new Vue({

  this.$http.get(url).then(function(res){

    //success

  },function(err){

    //error

  });

});

第二种是全局的Vue进行请求

Vue.http.get(url).then(function(res){

    //success

  },function(err){

    //error

  });

在这种方法中,当请求成功对返回的额数据进行页面渲染时,直接赋值给data是不行的,由于此处是全局请求,无法触发双向数据绑定,因此,必须要做一定的处理,即this.$set("items",res.data);

而且要注意此处的this关键字,比如在组件中使用全局的方法,则要在请求之前,把this取出来,如下

new Vue({

  data:{

    items:null

  },

  methods: {

    getList: function(){

      var _this = this;

      Vue.http.get(url).then(function(res){

        //success

        _this.$set("items",res.data);

      },function(err){

        //error

      });

    }

  }

});