在JavaScript端检测JSON中的更改

时间:2018-06-26 11:47:47

标签: javascript vue.js

我正在从PHP接收JSON格式的数据,我想检测JSON是否在Javascript方面而不是在模板方面已更改。有人有解决办法吗?我正在附上代码。

export default {
    name: 'dashboard_invoices',
    data() {
        return {
            invoices: [],
            newInvoices: [],
        }
    },
    methods: {
        loadDataInterval: function() {
            this.$http.get('http://127.0.0.1/pdaserwis-crm/api/vue/vue?action=order_table').then(function (response) {
                this.newInvoices = response.data;
                if(this.newInvoices != this.invoices) {
                    // alert('Dodano nowa fakture');
                    this.invoices = this.newInvoices;
                }
            })
        },
        loadData: function() {
            this.$http.get('http://website.pl').then(function (response) {
                this.invoices = response.data;
            })
        }
    },
    created: function () {
        this.loadData();

        setInterval(function () {
            this.loadDataInterval();
        }.bind(this), 3000);
    }
}

我想了解发票是否已更改,并查看相应的警报。

1 个答案:

答案 0 :(得分:0)

问题解决了。监视处理程序需要将两个数组与深相等数组进行比较。

watch: {
        invoices: {
            handler(val, oldVal)
                {
                    if(!(deepEqual(val, oldVal))) {
                        console.log('elo');
                    }
                }
        }
    }