0

I create Vue object and need update some data when get response

new Vue({
    el: "#app",
    data: {
        message: [1,2,3,]
    },
    methods: {
        getList: function () {
            var myGet = axios.get('/api/guests/.json')
                .then(function (response) {                    
// ????????? message = response.data;

                })
                .catch(function (error) {
                    console.log(error);
                });            
        }
    },
    created:function () {
        this.getList();
    }
});

how to update vue object value inside this object? how to update 'message' ?

1
  • this.data.message = response.data; Commented Jun 7, 2017 at 7:04

1 Answer 1

1

You need to bind the correct context to your then callback and access this.message. You will have to do something like this

new Vue({
    el: "#app",
    data: {
        message: [1,2,3,]
    },
    methods: {
        getList: function () {
            var myGet = axios.get('/api/guests/.json')
                .then(function (response) {                    
                    this.message = response.data;
                }.bind(this))
                .catch(function (error) {
                    console.log(error);
                });            
        }
    },
    created:function () {
        this.getList();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.