Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js: watch array length

How can I watch an array length using Vue.js?

like image 967
Alfred Huang Avatar asked May 05 '16 03:05

Alfred Huang


1 Answers

Use the watch section in your vm creation:

var vm = new Vue({
    el: 'body',
    data: {
        items: []
    },
    computed: {
        item_length: function () {
            return this.battle_logs.length;
        }
    },
    watch: {
        items: {
            handler: function () {
                console.log('caught!');
            },
            deep: true
        }
    }
});

Or watch a computed length attribute:

vm.$watch('item_length', function(newVal, oldVal) {
    console.log('caught!');
});
like image 158
Alfred Huang Avatar answered Nov 13 '22 02:11

Alfred Huang