0

In my vue.js project, i get an array data by axios, and want to assign to bookList variable, but failed, bookList still equal to [], could you tell me why?

export default {
...
data () {
  return {
    bookList: []
  }
},
mounted: function() {
  this.$nextTick(function(){
    this.viewBooks();
  });
},
methods: {
  viewBooks: function() {
    axios.get('/books.json')
      .then(res=>{
        this.bookList = res.data.bookList;
      })
      .catch(error=>{
        console.log(error);
      });
  }
}

1 Answers1

0

The callback is on a different scope...this should work

methods: {
    viewBooks: function() {
        let self = this;
        axios.get('/books.json')
            .then(res=>{
                self.bookList = res.data.bookList;
            })
            .catch(error=>{
                console.log(error);
            });
    }

There's also a different answer here

Murwa
  • 2,238
  • 1
  • 20
  • 21