this.$http.get 似乎不起作用 vue-resource
this.$http.get seems doesn't work vue-resource
使用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()
}
});
但最后
用户没有价值。
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()
}
});
检查变量作用域。将指针设置为 "this",如 "self"。
Vue-resource 是基于承诺的 API。
get 请求的语法应该是
this.$http.get('/someUrl')
.then(response => {
// get body data
this.someData = response.body;
}, err => {
// error callback
});
因为你已经在数据选项中初始化了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
})
}
使用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()
}
});
但最后
用户没有价值。
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()
}
});
检查变量作用域。将指针设置为 "this",如 "self"。
Vue-resource 是基于承诺的 API。 get 请求的语法应该是
this.$http.get('/someUrl')
.then(response => {
// get body data
this.someData = response.body;
}, err => {
// error callback
});
因为你已经在数据选项中初始化了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
})
}