当前位置:   article > 正文

watch & watchEffect Vue3

watch & watchEffect Vue3

watch的应用场景

1. 监视【ref】定义的【基本类型】数据

  1. <template>
  2. <div class="person">
  3. <h2>sum: {{ sum }}</h2>
  4. <button @click="Add">+1</button>
  5. </div>
  6. </template>
  7. <script lang="ts" setup name = "Person">
  8. import { ref, watch } from 'vue'
  9. //data
  10. let sum = ref(0)
  11. //function
  12. function Add() {
  13. sum.value += 1
  14. }
  15. //watch
  16. watch(sum, (newValue, oldValue) => {
  17. console.log(newValue, oldValue)
  18. })
  19. </script>
  20. <style scoped>
  21. /* 可以添加样式 */
  22. </style>

添加停止监视: 

  1. <template>
  2. <div class="person">
  3. <h2>sum: {{ sum }}</h2>
  4. <button @click="Add">+1</button>
  5. </div>
  6. </template>
  7. <script lang="ts" setup name = "Person">
  8. import { ref, watch } from 'vue'
  9. //data
  10. let sum = ref(0)
  11. //function
  12. function Add() {
  13. sum.value += 1
  14. }
  15. //watch
  16. const stopWatch = watch(sum, (newValue, oldValue) => { //监视sum
  17. console.log(newValue, oldValue)
  18. if (newValue >= 10){
  19. stopWatch() //需要前面定义 ‘ const stopWatch = ’
  20. }
  21. })
  22. </script>
  23. <style scoped>
  24. /* 可以添加样式 */
  25. </style>

2. 监视【ref】定义的【对象类型】数据

watch监视的是对象的地址值,即只有执行changePerson更改对象地址值后,才被监视到

若想监视对象内部属性的变化,需要手动开启深度监视

  1. <template>
  2. <div class="person">
  3. <h2>Name: {{ person.name }}</h2>
  4. <h2>Age: {{ person.age }}</h2>
  5. <button @click="changeName">Change Name</button>
  6. <button @click="changeAge">Change Age</button>
  7. <button @click="changePerson">Change Person</button>
  8. </div>
  9. </template>
  10. <script lang="ts" setup name = "Person">
  11. import { ref, watch } from 'vue'
  12. //data
  13. let person = ref({
  14. name: 'Tom',
  15. age: 18
  16. })
  17. //function
  18. function changeName() {
  19. person.value.name = 'Jack'
  20. }
  21. function changeAge() {
  22. person.value.age += 1
  23. }
  24. function changePerson() {
  25. person.value = {
  26. name:'Anna',
  27. age: 19
  28. }
  29. }
  30. //watch
  31. watch(
  32. person,
  33. (newValue, oldValue)=>{
  34. console.log(newValue, oldValue)
  35. },
  36. {deep: true} // {}这是一个配置对象,执行了:手动开启深度监视
  37. )
  38. </script>
  39. <style scoped>
  40. /* 可以添加样式 */
  41. </style>

3. 监视【reactive】定义的【对象类型】数据

  1. <template>
  2. <div class="person">
  3. <h2>Name: {{ person.name }}</h2>
  4. <h2>Age: {{ person.age }}</h2>
  5. <button @click="changeName">Change Name</button>
  6. <button @click="changeAge">Change Age</button>
  7. <button @click="changePerson">Change Person</button>
  8. </div>
  9. </template>
  10. <script lang="ts" setup name = "Person">
  11. import { reactive, watch } from 'vue'
  12. //data
  13. let person = reactive({
  14. name: 'Tom',
  15. age: 18
  16. })
  17. //function
  18. function changeName() {
  19. person.name = 'Jack'
  20. }
  21. function changeAge() {
  22. person.age += 1
  23. }
  24. function changePerson() {
  25. Object.assign (
  26. person,
  27. {
  28. name: 'Anna',
  29. age: 19
  30. }
  31. )
  32. }
  33. //watch,监视【reactive】定义的【对象类型】数据时默认开启深度监视
  34. watch(
  35. person,
  36. (newValue, oldValue) => {
  37. console.log(newValue, oldValue)
  38. }
  39. )
  40. </script>
  41. <style scoped>
  42. /* 可以添加样式 */
  43. </style>

4. 监视【ref】/【reactive】定义的【对象类型】数据中的某个属性

  1. <template>
  2. <div class="person">
  3. <h2>Name: {{ person.name }}</h2>
  4. <h2>Age: {{ person.age }}</h2>
  5. <h2>Hobby: {{ person.hobby.h1 }}, {{ person.hobby.h2 }}</h2>
  6. <button @click="changeName">Change Name</button>
  7. <button @click="changeAge">Change Age</button>
  8. <button @click="changeH1">Change Hobby1</button>
  9. <button @click="changeAllHobby">Change All Hobby</button>
  10. </div>
  11. </template>
  12. <script lang="ts" setup name = "Person">
  13. import { reactive, watch } from 'vue'
  14. // data
  15. let person = reactive({
  16. name: 'Tom',
  17. age: 18,
  18. hobby:{
  19. h1:'Swimming',
  20. h2:'Running'
  21. }
  22. })
  23. // function
  24. function changeName() {
  25. person.name += '~'
  26. }
  27. function changeAge() {
  28. person.age += 1
  29. }
  30. function changeH1() {
  31. person.hobby.h1 = 'Boxing'
  32. }
  33. function changeAllHobby() {
  34. // 这里不用object.assign,因为改的不是reactive对象person本身,改的是person对象里的Hobby对象
  35. person.hobby = {
  36. h1: 'Dancing',
  37. h2: 'Singing'
  38. }
  39. }
  40. // watch,监视Name属性
  41. watch(
  42. ()=>person.name, // 监视person.name,但不符合watch语法,只能写这个函数/ref/reactive/以上的数组
  43. (newValue, oldValue)=>{console.log(newValue, oldValue)}
  44. )
  45. // watch,监视hobby对象
  46. watch(
  47. ()=>person.hobby, //监视地址
  48. (newValue, oldValue)=>{console.log(newValue, oldValue)},
  49. {deep:true} // 同时监视其中的属性
  50. )
  51. </script>
  52. <style scoped>
  53. /* 可以添加样式 */
  54. </style>

 5. 监视多个属性——使用数组

  1. <template>
  2. <div class="person">
  3. <h2>Name: {{ person.name }}</h2>
  4. <h2>Age: {{ person.age }}</h2>
  5. <h2>Hobby: {{ person.hobby.h1 }}, {{ person.hobby.h2 }}</h2>
  6. <button @click="changeName">Change Name</button>
  7. <button @click="changeAge">Change Age</button>
  8. <button @click="changeH1">Change Hobby1</button>
  9. <button @click="changeAllHobby">Change All Hobby</button>
  10. </div>
  11. </template>
  12. <script lang="ts" setup name = "Person">
  13. import { reactive, watch } from 'vue'
  14. // data
  15. let person = reactive({
  16. name: 'Tom',
  17. age: 18,
  18. hobby:{
  19. h1:'Swimming',
  20. h2:'Running'
  21. }
  22. })
  23. // function
  24. function changeName() {
  25. person.name += '~'
  26. }
  27. function changeAge() {
  28. person.age += 1
  29. }
  30. function changeH1() {
  31. person.hobby.h1 = 'Boxing'
  32. }
  33. function changeAllHobby() {
  34. // 这里不用object.assign,因为改的不是reactive对象person本身,改的是person对象里的Hobby对象
  35. person.hobby = {
  36. h1: 'Dancing',
  37. h2: 'Singing'
  38. }
  39. }
  40. // watch,监视
  41. watch(
  42. [()=>person.name, ()=>person.hobby.h1],
  43. (newValue, oldValue)=>{
  44. console.log(newValue, oldValue)
  45. },
  46. {deep:true}
  47. )
  48. </script>
  49. <style scoped>
  50. /* 可以添加样式 */
  51. </style>

WatchEffect应用场景

官网:立即运行一个函数,同时响应式地追踪其依赖,并在依赖更改时重新执行该函数。

  1. <template>
  2. <div class="person">
  3. <h2>x: {{ x }}</h2>
  4. <h2>y: {{ y }}</h2>
  5. <button @click="plusX">x+1</button>
  6. <button @click="plusY">y+1</button>
  7. </div>
  8. </template>
  9. <script lang="ts" setup name = "Person">
  10. import { ref, watchEffect } from 'vue'
  11. // data
  12. let x = ref(0)
  13. let y = ref(0)
  14. // function
  15. function plusX() {
  16. x.value += 1
  17. }
  18. function plusY() {
  19. y.value += 1
  20. }
  21. // watch,监视
  22. watchEffect(
  23. ()=>{
  24. if(x.value >= 3 || y.value >= 3) {
  25. console.log('message')
  26. }
  27. }
  28. )
  29. </script>
  30. <style scoped>
  31. /* 可以添加样式 */
  32. </style>

watch 对比watchEffect

1. 都能监听响应式数据的变化,不同的是监听数据变化的方式不同
2. watch:要明确指出监视的数据
3. watchEffect:不用明确指出监视的数据(函数中用到哪些属性,那就监视哪些属性)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/290783
推荐阅读
相关标签
  

闽ICP备14008679号