Vue使用watch监听数组或对象

admin4年前vue598

1、普通的watch

1
2
3
4
5
6
7
8
9
10
data() {
    return {
        frontPoints: 0  
    }
},
watch: {
    frontPoints(newValue, oldValue) {
        console.log(newValue)
    }
}

2、数组的watch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
data() {
    return {
        winChips: new Array(11).fill(0) 
    }
},
watch: {
  winChips: {
    handler(newValue, oldValue) {
      for (let i = 0; i < newValue.length; i++) {
        if (oldValue[i] != newValue[i]) {
          console.log(newValue)
        }
      }
    },
    deep: true
  }
}

3、对象的watch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
data() {
  return {
    bet: {
      pokerState: 53,
      pokerHistory: 'local'
    } 
    }
},
watch: {
  bet: {
    handler(newValue, oldValue) {
      console.log(newValue)
    },
    deep: true
  }
}


相关文章

vue2使用富文本编辑器

https://blog.csdn.net/m0_61257622/article/details/127331859...

vue自定义组件实现v-model(含vue3)

https://www.cnblogs.com/cl1998/p/15953250.html...

在VUE中使用swiper

npm i swiper<template>   <swiper     :slides-per-...

vue elementui动态生成表格数据

<el-table :data="tableData" border class="mb30">       ...

vue3 ssr

https://zhuanlan.zhihu.com/p/565464937?utm_id=0...

$nextTick()的理解

简单理解Vue中的nextTick 简单理解Vue中的nextTick$nextTick()的理解 $nextTick()的理解 错误示例1:test.html<!DOCTYPE html>...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。