Vue中 axios 发送异步请求后,对VUE的操作错误

在 axios 请求后的处理,需要先将 VUE 的 this 对象保存起来,不能直接在axios里用this对象

let _this;
let vm = new Vue({
    el: "#app",
    data: {
        deleted: false,
    },
    beforeMount: function () {
        _this = this;
    },
    methods: {
        del(id) {
            // 使用拼接进行删除
            axios.delete("/del?id=" + id).then(function (res) {
                console.log(res.data);
                _this.deleted = true;   // 不能直接使用 this
            }).catch(function (err) {
                console.log(err);
            });
        },
    }
});

参考链接:vue 点击按钮事件,采用axios异步获取数据后,页面没有更新数据

发表评论