Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic vue.js 2 and vue-resource http get with variable assignment

I'm really struggling to get the most basic REST functionality to work in vue.js 2.

I'd like to get data from some endpoint and assign the returned value to a variable of my Vue instance. Here's how far I got.

var link = 'https://jsonplaceholder.typicode.com/users';
var users;

Vue.http.get(link).then(function(response){
    users = response.data;
}, function(error){
    console.log(error.statusText);
});

new Vue ({
    el: '#user-list',
    data: {
        list: users
    }
});

Inside the promise, I can access the response data, but I cannot seem to assign it to users or even to data of the Vue instance.

Needless to say, I'm completely new to vue.js and thankful for any help.

Stack: vue.js 2.03, vue-resource 1.0.3

Cheers!

like image 564
Robin Löffel Avatar asked Oct 16 '16 10:10

Robin Löffel


People also ask

How to make an API call with Vue JS components?

1. Create the Basic Skeleton Of a Web Application 2. Create the Basic Vue.js Component 3. Create the Stylesheet for the Vue Component 4. Add API call to Vue.js Component 5. Display API Data In Vue.js Component 6. Styling the Vue.js Component What is Vue.js? What are Vue.js Components? How to Make an API Call with Vue.js?

How to create a Vue resource in VUE web app?

Step 1: Create a project folder. Now, create one project folder called VueResource. In root, create one file call package.json and copy the following code to it.

How to send the data to the Backend API using Vue resource?

The POST request method is used to send the data to the backend API, let’s see an example. In the above code, this.$http.post () method takes two arguments, the first argument is the api url and second argument is the data we need to send to the api. In vue resource we can also set global root url instead of adding a full url to every request.

How to make HTTP requests using vueresource?

By adding this configuration VueResource provides us a global $http object, which helps us to make http requests anywhere from our Vue app. For learning purposes, we are using json placeholder API for making http requests. GET request method is used to fetch the data from the backend API, let’s see an example.


1 Answers

You can create an object and pass it to the vue instance like that :

var link = 'https://jsonplaceholder.typicode.com/users';
var data = { 
    list: null 
};

Vue.http.get(link).then(function(response){
    data.list = response.data;
}, function(error){
    console.log(error.statusText);
});

new Vue ({
    el: '#app',
    data: data 
});

Or you can create a function under the methods object and then call it in the mounted function :

var link = 'https://jsonplaceholder.typicode.com/users';
new Vue ({
    el: '#app',
    data: {
        list: null
    },
    methods:{
        getUsers: function(){
            this.$http.get(link).then(function(response){
                this.list = response.data;
            }, function(error){
                console.log(error.statusText);
            });
        }
    },
    mounted: function () {
        this.getUsers();
    }
});
like image 192
ABDEL-RHMAN Avatar answered Oct 13 '22 06:10

ABDEL-RHMAN