[Vuejs]-How do I assign an array of objects to an empty array in a Vue component?

2πŸ‘

βœ…

The closure of this in function(){ //code } is the function itself. If you want to access the this of the object which you defined the function you will need to use arrow function as follow.

var obj = {
    getToDos() {
       // this here refer to obj
       axios.get('/todo')
        .then( res =>  {
           this.todos =  res.data
       })
    }

}

More information about closure in js:
https://www.w3schools.com/js/js_function_closures.asp
https://medium.com/@vmarchesin/javascript-arrow-functions-and-closures-4e53aa30b774

πŸ‘€HW Siew

1πŸ‘

Use arrow function. Like this for .then part

.then((res) => {
    //rest of code
})
πŸ‘€Dhrumil

Leave a comment