English

一篇解决vue3中watch的一切困惑

7/1/2024
18

开篇先说结论:

1. 如果监听的是整个object, 那必须用 deep watch,否则没有任何用

2. 如果监听的是primitive(基础类型或者值拷贝类型 bool, string, null, number, undefined)的属性,默认就是 {deep: false}, 而且不需要使用{deep: true}

3. 如果监听的是nested(array或者object)的属性,

a. 当{deep: false}, 当这个nested 的引用被替换的时候可以被监听到, 但是如果它里面的property的值进行了更新,是无法监听到的

b. 使用{deep: true} : 当你需要监听 nested property里面的值

数据准备

JavaScript
let user = reactive({
  name: 'John',  //name就是primitive 类型的属性
  age: 25,
  address: {  //address 就是 nested 类型的属性, 因为他的值是 object 
    city: 'New York',
    zip: '10001'
  }
});

案例 1 :

直接监听整个对象也就是文中的 user,{deep: false}, 什么都不会监听到,什么作用也没有

1. 更改primitive 属性,name ❌

2. 更改nested 属性, address里面的值 ❌

3. 新添加一个property ❌

4. 直接重新给赋值一个新的对象, 这个对象没有响应式, 破坏了响应式

5. ❌直接重新赋值一个新的对象,并且这个对象有响应式 ❌

JavaScript
<script setup>
import { reactive, watch } from 'vue';

// Create a reactive object
let user = reactive({
  name: 'John',
  age: 25,
  address: {
    city: 'New York',
    zip: '10001'
  }
});


watch(
  () => user,
  (newValue, oldValue) => {
    console.log('User object changed:', newValue);
  }, 
  {deep: false}
);

const wholeR= ()=>{
  user = reactive({ 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  })
  console.log(user);

}

const whole= ()=>{
  user = { 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  }
console.log(user);
}

const newPro = () =>{
  user.newPro ='updated pro'
}


</script>

<template>
<h1>{{user.name }}</h1>
<h1>{{ user.address.city }}</h1>

<button @click="user.name = 'Jane'" > update name </button>
<button  @click="user.address.city = 'Los Angeles';" > update city nested </button>
<button  @click="wholeR" > update the whole object with reactive</button>
<button  @click="whole" > update the whole object</button>
<button  @click="newPro" > add new property</button>

</template>

案例2: 直接监听整个对象也就是文中的 user,{deep: true}, 可以监听到1-3的情况,4-5无法监听到

1. 更改primitive 属性,name -OK ✅

2. 更改nested 属性, address里面的值 --OK ✅

3. 新添加一个property --OK✅

4. 直接重新给赋值一个新的对象, 这个对象没有响应式, 破坏了响应式 -BAD (没有监听到)❌

5. 直接重新赋值一个新的对象,并且这个对象有响应式 - BAD (没有监听到)❌

JavaScript
<script setup>
import { reactive, watch } from 'vue';

// Create a reactive object
let user = reactive({
  name: 'John',
  age: 25,
  address: {
    city: 'New York',
    zip: '10001'
  }
});


watch(
  () => user,
  (newValue, oldValue) => {
    console.log('User object changed:', newValue);
  }, 
  {deep: true}
);


const wholeR= ()=>{
  user = reactive({ 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  })
  console.log(user);

}

const whole= ()=>{
  user = { 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  }
console.log(user);
}

const newPro = () =>{
  user.newPro ='updated pro';
  console.log(user);
}


</script>

<template>
<h1>{{user.name }}</h1>
<h1>{{ user.address.city }}</h1>

<button @click="user.name = 'Jane'" > update name </button>
<button  @click="user.address.city = 'Los Angeles';" > update city nested </button>
<button  @click="newPro" > add new property</button>
<button  @click="wholeR" > update the whole object with reactive</button>
<button  @click="whole" > update the whole object</button>


</template>

案例3:直接监听具体的基础类型属性,{deep: false} , (没有必要deep: true 就不演示了)

1. 更改primitive 属性,name -OK✅

2. 更改nested 属性, address里面的值 - BAD (没有监听到)❌

3. 新添加一个property - BAD (没有监听到)❌

4. 直接重新给赋值一个新的对象, 这个对象没有响应式, 破坏了响应式 -BAD (没有监听到)❌

5. 直接重新赋值一个新的对象,并且这个对象有响应式 - BAD (没有监听到)❌

JavaScript
<script setup>
import { reactive, watch } from 'vue';

// Create a reactive object
let user = reactive({
  name: 'John',
  age: 25,
  address: {
    city: 'New York',
    zip: '10001'
  }
});


watch(
  () => user.name,
  (newValue, oldValue) => {
    console.log('User object changed:', newValue);
  }
);


const wholeR= ()=>{
  user = reactive({ 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  })
  console.log(user);

}

const whole= ()=>{
  user = { 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  }
console.log(user);
}

const newPro = () =>{
  user.newPro ='updated pro';
  console.log(user);
}


</script>

<template>
<h1>{{user.name }}</h1>
<h1>{{ user.address.city }}</h1>

<button @click="user.name = 'Jane'" > update name </button>
<button  @click="user.address.city = 'Los Angeles';" > update city nested </button>
<button  @click="newPro" > add new property</button>
<button  @click="wholeR" > update the whole object with reactive</button>
<button  @click="whole" > update the whole object</button>

</template>

案例4:直接监听具体的nested类型属性,{deep: false}

1. 直接更改nested属性的值,相当于换了新的reference, - 监听到了 ✅

2. 更改primitive 属性,name - BAD (没有监听到) ❌

3. 更改nested 属性, address里面的值 - BAD (没有监听到)❌

4. 新添加一个property - BAD (没有监听到)❌

5. 直接重新给赋值一个新的对象, 这个对象没有响应式, 破坏了响应式 -BAD (没有监听到)❌

6. 直接重新赋值一个新的对象,并且这个对象有响应式 - BAD (没有监听到)❌

JavaScript
<script setup>
import { reactive, watch } from 'vue';

// Create a reactive object
let user = reactive({
  name: 'John',
  age: 25,
  address: {
    city: 'New York',
    zip: '10001'
  }
});


watch(
  () => user.address,
  (newValue, oldValue) => {
    console.log('User object changed:', newValue);
  }
);

const wholeR= ()=>{
  user = reactive({ 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  })
  console.log(user);

}

const whole= ()=>{
  user = { 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  }
console.log(user);
}

const newPro = () =>{
  user.newPro ='updated pro';
  console.log(user);
}

const updateAddressWhole = () =>{
  user.address ={
    country:'china'
  }
}



</script>

<template>
<h1>{{user.name }}</h1>
<h1>{{ user.address.city }}</h1>

<button  @click="updateAddressWhole" > updateAddressWhole</button>
<button @click="user.name = 'Jane'" > update name </button>
<button  @click="user.address.city = 'Los Angeles';" > update city nested </button>
<button  @click="newPro" > add new property</button>
<button  @click="wholeR" > update the whole object with reactive</button>
<button  @click="whole" > update the whole object</button>


</template>

案例5:直接监听具体的nested类型属性,{deep: true}

1. 直接更改nested属性的值,相当于换了新的reference, - 监听到了 ✅

2. 更改nested 属性, address里面的值 - - 监听到了 ✅

3. 更改primitive 属性,name - BAD (没有监听到) ❌

4. 新添加一个property - BAD (没有监听到)❌

5. 直接重新给赋值一个新的对象, 这个对象没有响应式, 破坏了响应式 -BAD (没有监听到) ❌

6. 直接重新赋值一个新的对象,并且这个对象有响应式 - BAD (没有监听到) ❌

JavaScript
<script setup>
import { reactive, watch } from 'vue';

// Create a reactive object
let user = reactive({
  name: 'John',
  age: 25,
  address: {
    city: 'New York',
    zip: '10001'
  }
});


watch(
  () => user.address,
  (newValue, oldValue) => {
    console.log('User object changed:', newValue);
  },{deep:true}
);

// watch(
//   () => user.address,
//   (newValue, oldValue) => {
//     console.log('User object changed:', newValue);
//   },
//   { deep: true }
// );

const wholeR= ()=>{
  user = reactive({ 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  })
  console.log(user);
//  console.log(user);
  // console.dir(user);
}

const whole= ()=>{
  user = { 
    name: 'major',
  age: 30,
  address: {
    city: 'Old York',
    zip: '10001'
  }

  }
console.log(user);
}

const newPro = () =>{
  user.newPro ='updated pro';
  console.log(user);
}

const updateAddressWhole = () =>{
  user.address ={
    country:'china'
  }
}



</script>

<template>
<h1>{{user.name }}</h1>
<h1>{{ user.address.city }}</h1>



<button  @click="user.address.city = 'Los Angeles';" > update city nested </button>
<button  @click="updateAddressWhole" > updateAddressWhole</button>
<button @click="user.name = 'Jane'" > update name </button>
<button  @click="newPro" > add new property</button>
<button  @click="wholeR" > update the whole object with reactive</button>
<button  @click="whole" > update the whole object</button>


</template>

结论: 

1. 如果监听的是整个object, 那必须用 deep watch,否则没有任何用

2. 如果监听的是primitive(基础类型或者值拷贝类型 bool, string, null, number, undefined)的属性,默认就是 {deep: false}, 而且不需要使用{deep: true}

3. 如果监听的是nested(array或者object)的属性,

a. 当{deep: false}, 当这个nested 的引用被替换的时候可以被监听到, 但是如果它里面的property的值进行了更新,是无法监听到的

b. 使用{deep: true} : 当你需要监听 nested property里面的值