【问题标题】:how to update vue object value inside this object如何更新此对象内的 vue 对象值
【发布时间】:2017-11-08 09:14:28
【问题描述】:

我创建了 Vue 对象并且需要在得到响应时更新一些数据

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();
    }
});

如何更新这个对象内的 vue 对象值? 如何更新“消息”?

【问题讨论】:

  • this.data.message = response.data;

标签: javascript rest api vue.js axios


【解决方案1】:

您需要将正确的上下文绑定到您的then 回调并访问this.message。你将不得不做这样的事情

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();
    }
});

【讨论】:

    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 2017-04-25
    • 2017-09-01
    • 2023-01-04
    • 1970-01-01
    • 2019-05-25
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多