vue-resource和vue-async-data两个插件的使用

时间:2016-01-23 22:57:08   收藏:0   阅读:19319

vue-resource和vue-async-data两个插件的使用,看了一下文档http://cn.vuejs.org/guide/plugins.html#u5DF2_u6709_u63D2_u4EF6__26amp_3B__u5DE5_u5177

var Vue = require("vue"),
    App = require("./App.vue");
var vueResource = require(vue-resource);
Vue.use(vueResource);

var vm = new Vue({
    el: body,
    ready: function(){
        this.$http.get(./src/test.json, {}, {
            headers: {
                "X-Requested-With": "XMLHttpRequest"
            },
            emulateJSON: true
        }).then(function(response) {
            var data = response.data;
            this.msg = data;
        }, function(response) {
            // handle error
        });
    },
    data: {
        msg: "hello",
        dom: "body"
    }
});

简单来说, vue-resource就像jQuery里的$.ajax, 用来和后端交互数据的...你可以放在created或者ready里面运行来获取或者更新数据...

vue-async-data应该是封装了下更新数据的方法, 不过还是需要vue-resource去做交互

var Vue = require("vue"),
    App = require("./App.vue");
var vueResource = require(vue-resource);
var VueAsyncData = require(vue-async-data)
Vue.use(vueResource);
Vue.use(VueAsyncData);

var vm = new Vue({
    el: body,
    asyncData: function (resolve, reject) {
        this.$http.get(./src/test.json, {}, {
            headers: {
                "X-Requested-With": "XMLHttpRequest"
            },
            emulateJSON: true
        }).then(function(response) {
            var data = response.data;
            resolve({
                msg: data
            });
        }, function(response) {
            // handle error
        });
    },
    data: {
        msg: "hello",
        dom: "body"
    }
});

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!