簡體   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