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

时间:2022-08-05 15:25:47

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