TypeError: this.$set is not a function

2020-07-22 19:26发布

Hi I am geting this error: Uncaught (in promise) TypeError: this.$set is not a function

And here is the code:

     export default {
        data: function() {
            return { movies: '' }
        },

        ready: function() {
            this.showMovies()
        },

        methods: {
            showMovies: function() {
                 this.$http.get(config.api.url + '/movies').then(function (response) {
                    this.$set('movies', response.data)
                 })
             }
        }
     }

标签: vue.js
1条回答
我想做一个坏孩纸
2楼-- · 2020-07-22 19:57

The reason why this.$set is not a function in your example code is because this doesn't refer to Vue ViewModel instance anymore.

To make code you've posted working, you need to keep reference to it:

export default {
    data: function() {
        return { movies: '' }
    },

    ready: function() {
        this.showMovies()
    },

    methods: {
        showMovies: function() {
             var vm = this; // Keep reference to viewmodel object
             this.$http.get(config.api.url + '/movies').then(function (response) {
                vm.$set('movies', response.data)
             })
         }
    }
 }
查看更多
登录 后发表回答