[Vuejs]-This.$http.get seems doesn't work vue-resource

4👍

Vue-resource is a promise based API.
The syntax for the get request should be

this.$http.get('/someUrl')
    .then(response => { 
        // get body data
        this.someData = response.body; 
    }, err => { 
        // error callback 
    }); 

Since you have initialized users: [ ] in the data option , no need to use Vue.$set you can directly assign the value using this.users = data

So do it like this:

fetchUser: function(){
      this.$http.get('http://localhost:8000/api/api/users')
        .then((data) => {
            this.users = data;
        }, err => {
            // handle error 
        })
}

0👍

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

Check variable scope. Set pointer to “this” like “self”.

Leave a comment