使用vue 2.1.10 vue-resource 1.3.4来获取数据:
const app = new Vue({
el: '#UserController',
data:{
users : [],
},
methods:{
fetchUser: function(){
this.$http.get('http://localhost:8000/api/api/users', function(data){
this.$set('users',data)
})
}
},
mounted(){
this.fetchUser()
}
});但最终
用户没有价值。
发布于 2017-07-12 16:49:41
Vue-resource是一个基于promise的API。get请求的语法应为
this.$http.get('/someUrl')
.then(response => {
// get body data
this.someData = response.body;
}, err => {
// error callback
}); 由于已在data选项中初始化了users: [ ],因此无需使用Vue.$set即可使用this.users = data直接赋值
所以你可以这样做:
fetchUser: function(){
this.$http.get('http://localhost:8000/api/api/users')
.then((data) => {
this.users = data;
}, err => {
// handle error
})
}发布于 2017-07-12 15:13:47
const app = new Vue({
el: '#UserController',
data:{
users : [],
},
methods:{
fetchUser: function(){
var self = this;
this.$http.get('http://localhost:8000/api/api/users', function(data){
self.$set(self,'users',data)
})
}
},
mounted(){
this.fetchUser()
}
});检查变量作用域。将指针设置为类似于"self“的"this”。
https://stackoverflow.com/questions/45050331
复制相似问题