点._vue3如何用computed函数去监听一个对象的变化">
当前位置:   article > 正文

Vue3中的组合API:computed函数,watch函数_vue3如何用computed函数去监听一个对象的变化

vue3如何用computed函数去监听一个对象的变化

一.计算属性:computed

1.计算属性中要有return

2.计算属性不能做异步操作,因为他是同步的,异步的不能通过返回值来被处理

3.如果使用修改数据set方法与读取数据get方法,computed({})中的参数需要是对象的形式,其中set会自动获取你设置的值,get中要有return,set中没有return

 1.基本使用的代码:

  1. <template>
  2. <h1>计算属性</h1>
  3. <button @click="age = 28">点击</button>
  4. <div>今年:{{age}}</div>
  5. <div>明年:{{nextage}}</div>
  6. </template>
  7. <script>
  8. import { ref, computed } from 'vue'
  9. export default {
  10. name: 'App',
  11. setup () {
  12. const age = ref(18)
  13. const nextage = computed(() =>
  14. age.value + 1
  15. )
  16. return {
  17. age,
  18. nextage
  19. }
  20. }
  21. }
  22. </script>

set与get方法

  1. <template>
  2. <h1>计算属性</h1>
  3. <button @click="age = 28">点击修改今年年龄</button>
  4. <button @click="nextage = 40">点击修改明年年龄</button>
  5. <div>今年:{{age}}</div>
  6. <div>明年:{{nextage}}</div>
  7. </template>
  8. <script>
  9. import { ref, computed } from 'vue'
  10. export default {
  11. name: 'App',
  12. setup () {
  13. const age = ref(18)
  14. const nextage = computed({
  15. get () {
  16. return age.value + 1
  17. },
  18. set (v) {
  19. age.value = v - 1
  20. }
  21. })
  22. return {
  23. age,
  24. nextage
  25. }
  26. }
  27. }
  28. </script>

二.watch函数(侦听器)

侦听器:被真的信息变化时,触发侦听器的回调函数,

典型的场景:路由参数发生变化,重新调用用接口获取组件的数据

1.监听ref定义的响应式数据(简单数据类型用的多)

 

  1. <template>
  2. <h1>侦听器</h1>
  3. <h2>{{count}}</h2>
  4. <button @click="count += 1">点击</button>
  5. </template>
  6. <script>
  7. import { ref, watch } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const count = ref(0)
  12. watch(count, (newValue, oldValue) => {
  13. console.log(newValue, oldValue)
  14. })
  15. return {
  16. count
  17. }
  18. }
  19. }
  20. </script>

2.监听reactive定义的响应式数据(复杂数据类型较多)

  注意:

1).监听对象中的某一个属性的时候watch里面要使用箭头函数指向对象中的属性

  1. <template>
  2. <h1>侦听器侦听对象</h1>
  3. <h2>{{obj.info}}</h2>
  4. <button @click="handlerClick">点击</button>
  5. </template>
  6. <script>
  7. import { watch, reactive, toRefs } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const obj = reactive({
  12. info: '中国',
  13. msg: '加纳'
  14. })
  15. const { info } = toRefs(obj)
  16. const handlerClick = () => {
  17. info.value = '中国加油'
  18. }
  19. // 监听对象中的某一个属性的时候watch里面要使用箭头函数指向对象中的属性
  20. watch(() => obj.info, (v, v2) => {
  21. console.log(v, v2)
  22. })
  23. return {
  24. obj,
  25. handlerClick
  26. }
  27. }
  28. }
  29. </script>

 2).监听整个对象的时候(如果想看新对象与旧对象的对比可以使用{...obj}浅拷贝来实现)

  1. <template>
  2. <h1>侦听器侦听对象</h1>
  3. <h2>{{obj.info}}</h2>
  4. <button @click="handlerClick">点击</button>
  5. </template>
  6. <script>
  7. import { watch, reactive, toRefs } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const obj = reactive({
  12. info: '中国',
  13. msg: '加纳'
  14. })
  15. const { info } = toRefs(obj)
  16. const handlerClick = () => {
  17. info.value = '中国加油'
  18. }
  19. // 监听obj整个的时候里面直接写,不用写函数的形式,否则不好使
  20. // watch(obj, (v, v2) => { // 前后对比的v与v2都是一个值,因为他是引用数据类型
  21. // console.log('新值', v)
  22. // console.log('旧值', v2)
  23. // })
  24. watch(() => { return { ...obj } }, (v, v2) => { // 这是能够看到修改之后的前后对比
  25. console.log('新值', v)
  26. console.log('旧值', v2)
  27. })
  28. return {
  29. obj,
  30. handlerClick
  31. }
  32. }
  33. }
  34. </script>

 3).监听数值中的多个响应式数据

  1. <template>
  2. <h1>侦听器侦听数组</h1>
  3. <h2>{{n1+"-----"+n2}}</h2>
  4. <button @click="handlerClick">点击</button>
  5. </template>
  6. <script>
  7. import { watch, ref } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const n1 = ref(1)
  12. const n2 = ref(2)
  13. const handlerClick = () => {
  14. n1.value = '从1改变了其他的'
  15. }
  16. // 监听数组的时候,里面直接写也不用写成函数的形式
  17. watch([n1, n2], (v, v2) => {
  18. console.log(v)
  19. console.log(v2)
  20. })
  21. return {
  22. n1,
  23. n2,
  24. handlerClick
  25. }
  26. }
  27. }
  28. </script>

 4).监听正个数组

  1. <template>
  2. <h1>侦听器侦听数组</h1>
  3. <!-- <h2>{{n1+"-----"+n2}}</h2>-->
  4. <button @click="handlerClick">点击</button>
  5. </template>
  6. <script>
  7. import { watch, reactive } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const arr = reactive([1, 2])
  12. const handlerClick = () => {
  13. arr[0] = 2
  14. }
  15. // 监听数组的时候,里面直接写也不用写成函数的形式
  16. // watch(arr, (v, v2) => {
  17. // console.log('新值:', v)
  18. // console.log('旧值:', v2)
  19. // })
  20. // 这可以对比出数组的前后不一样的值,使用浅拷贝
  21. watch(() => [...arr], (v, v2) => {
  22. console.log('新值:', v)
  23. console.log('旧值:', v2)
  24. })
  25. return {
  26. arr,
  27. handlerClick
  28. }
  29. }
  30. }
  31. </script>

 3.使用参数(immediate,deep)

  1. <template>
  2. <h1>侦听器中的参数形式</h1>
  3. <div>{{obj.friend.name}}</div>
  4. <button @click="handlerClick">点击</button>
  5. </template>
  6. <script>
  7. import { watch, reactive } from 'vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. const obj = reactive({
  12. n1: '哈哈',
  13. n2: '嘿嘿',
  14. friend: {
  15. name: '婷婷',
  16. age: 18
  17. }
  18. })
  19. const handlerClick = () => {
  20. obj.friend.name = '小明'
  21. }
  22. watch(() => obj.friend, (v, v2) => {
  23. console.log('忠于自己')
  24. }, { deep: true })
  25. return {
  26. obj,
  27. handlerClick
  28. }
  29. }
  30. }
  31. </script>

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