Vue.js:手表数组长度

Vue.js: watch array length

本文关键字:数组 手表 js Vue      更新时间:2023-09-26

如何使用Vue.js查看数组长度?

在vm创建中使用watch部分:

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

或者观看计算长度属性:

vm.$watch('item_length', function(newVal, oldVal) {
    console.log('caught!');
});

在vue3设置中查看items.length

import { watch } from "vue";
watch(
  () => items.length,
  (newValue,oldValue) => { console.log(newValue,oldValue)}
)