简体   繁体   English

在JavaScript端检测JSON中的更改

[英]Detect changes in JSON on a javascript side

I am receiving data in JSON from PHP and I want to detect if JSON have changed on Javascript side, not template side. 我正在从PHP接收JSON格式的数据,我想检测JSON是否在Javascript端而不是模板端发生了变化。 Does anybody have solution? 有人有解决办法吗? I am attaching a code. 我正在附上代码。

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

I want to catch if invoices have changed and view appropriate alert for that. 我想了解发票是否已更改,并查看相应的警报。

The problem solved. 问题解决了。 It took to compare both arrays with deep-equal by watch handler. 监视处理程序将两个数组与深相等数组进行了比较。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM