繁体   English   中英

Vue.js:观察数组长度

[英]Vue.js: watch array length

如何使用 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)}
)

暂无
暂无

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

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