[Vuejs]-VueJs method syntax

0πŸ‘

βœ…

  • The ready hook is for Vue 1. In version 2 you need to use either created or mounted.
  • Instead of using this.$set, you should declare movies upfront in the data object.
  • Are you using vue-resource? I think your this.$http.get call may be incorrect; I don’t think it accepts a callback, it returns a Promise. Also the function is not bound to this which may be a problem (use arrow function syntax instead).

Modified for Vue 2:

new Vue({
    el: '#app',

    data: {
        movies: [],
    },

    mounted() {
        this.getMovies();
    },

    methods: {
        getMovies() {
            this.$http.get(apiURL).then(res => {
                this.movies = res.data;
            });
        }
    }
})

Otherwise if you’re using Vue 1 then just keep ready as-is.

Leave a comment