javascript - 如何更新此对象内的 vue 对象值

标签 javascript rest api vue.js axios

我创建 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 对象值? 如何更新“消息”?

最佳答案

您需要将正确的上下文绑定(bind)到 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();
    }
});

关于javascript - 如何更新此对象内的 vue 对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44405502/

相关文章:

javascript - 如何隐藏 jquery 中所有相关的列

javascript - Cassandra - 类型错误 : "value" argument is out of bounds

javascript - 如何从java读取Bookmarklet?

java - @Post Jersey 在带有 javax.ws.rs.POST 注释的方法中只允许有一个未注释的参数

node.js 无法使用 http 进行 rest 调用

.net - 应该为批处理作业提供什么 HTTP 响应代码和 header ?

api - Shopware 产品 API 响应为 "Cannot unset string offsets"

api - 尝试查询 API,但 api 响应为空

node.js - 在 Sequelize 和 postgres 中插入和获取 JSON 数据

php - 将链接中的文本值定位为一个输出中的多个链接