当前位置:   article > 正文

Vue3 第四节 自定义hook函数以及组合式API_vue hooks api

vue hooks api

1.自定义hook函数

2.toRef和toRefs

3.shallowRef和shallowReactive

4.readonly和shallowReadonly

5.toRaw和markRaw

6.customref

一.自定义hook函数

① 本质是一个函数,把setup函数中使用的Composition API 进行了封装,类似于vue2.x中的mixin

    自定义hook函数可以进行代码复用,让setup中的逻辑更清晰

② 代码实现

(1)新建hooks

(2) 将相关功能和接口写入

usePoint.js

  1. import { reactive, onMounted, onBeforeUnmount } from "vue"
  2. export default function () {
  3. let point = reactive({
  4. x: 0,
  5. y: 0
  6. })
  7. function savePoint (event) {
  8. console.log(event.pageX, event.pageY)
  9. point.x = event.pageX
  10. point.y = event.pageY
  11. }
  12. onMounted(() => {
  13. window.addEventListener('click', savePoint)
  14. })
  15. onBeforeUnmount(() => {
  16. window.removeEventListener('click', savePoint)
  17. })
  18. return point
  19. }

 (3)引入和使用

(4)代码汇总

Demo.vue

  1. <template>
  2. <h2>当前求和为:{{ sum }}</h2>
  3. <button @click="sum++">点我+1</button>
  4. <hr />
  5. <h2>当前点击时鼠标的坐标为:x : {{ point.x }} y:{{ point.y }}</h2>
  6. </template>
  7. <script>
  8. import usePoint from '../hooks/usePoint'
  9. import { reactive, ref, onMounted, onBeforeUnmount } from 'vue'
  10. export default {
  11. name: 'Demo',
  12. // 数据
  13. setup () {
  14. let sum = ref(0)
  15. let point = usePoint()
  16. return {
  17. sum,
  18. point
  19. }
  20. },
  21. }
  22. </script>
  23. <style>
  24. </style>

Test.vue

  1. <template>
  2. <h2>我是Test组件</h2>
  3. <h2>当前点击时鼠标的坐标为:x : {{ point.x }} y:{{ point.y }}</h2>
  4. </template>
  5. <script>
  6. import usePoint from '../hooks/usePoint'
  7. export default {
  8. name: 'Test',
  9. setup () {
  10. const point = usePoint()
  11. return { point }
  12. }
  13. }
  14. </script>

二. toRef和toRefs

① 作用:创建一个ref对象,其value值指向另一个对象中的某个属性

② 应用: 要将响应式对象中的某个属性单独提供给外部使用,不想丢失响应式

toRef与toRefs功能一致,toRefs可以批量创建多个ref对象,处理这个对象中的所有属性

 ③ 代码

  1. <template>
  2. <h4>{{ person }}</h4>
  3. <h2>姓名:{{ name }}</h2>
  4. <h2>年龄:{{ age }}</h2>
  5. <h2>薪资:{{ job.j1.salary }}</h2>
  6. <button @click="name += '~'">修改姓名</button>
  7. <button @click="age++">增长年龄</button>
  8. <button @click="job.j1.salary++">涨薪</button>
  9. </template>
  10. <script>
  11. import { reactive, ref, toRef, toRefs } from 'vue'
  12. export default {
  13. name: 'Demo',
  14. // 数据
  15. setup () {
  16. let person = reactive({
  17. name: '张三',
  18. age: 18,
  19. job: {
  20. j1: {
  21. salary: 20
  22. }
  23. }
  24. })
  25. return {
  26. person,
  27. // name: toRef(person, 'name'),
  28. // age: toRef(person, 'age'),
  29. // salary: toRef(person.job.j1, 'salary')
  30. ...toRefs(person)
  31. }
  32. }
  33. }
  34. </script>
  35. <style>
  36. </style>

三.shallowRef和shallowReactive

引入:import { shallowReactive, shallowRef } from 'vue'

① shallowReactive:只处理对象最外层属性的响应式(浅响应式)

 ② shallowRef: 只处理基本数据类型的响应式,不进行对象的响应式处理

 ③ 使用场景:

  • 如果有一个对象数据,结构比较深,但变化只是外层属性变化的时候使用shallowReactive
  • 如果有一个对象数据,后续功能不会修改对象中的属性,而是生成新的对象替换 =>  shallowRef

 四.readonly和shallowReadonly

应用场景:不希望数据被修改

① readonly: 让一个响应式数据变成只读的(深只读)

 ② shallowReadonly:只限制对象中的第一层数据

对ref的对象同样有用

 五.toRaw和markRaw

 ① toRaw

  • 作用:将一个由reactive生成的响应式对象转为普通对象
  • 使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引起页面更新

 ② markRaw

  • 作用:标记一个对象,使其永远不会再成为响应式对象
  • 应用场景

     1.有些值不应该被设置为响应式的,例如复杂的第三方类库等

     2.当渲染具有不可变数据源的大列表时,跳过响应式转换可以提高性能

 操作数据,数据确实发生变化了,但是界面不会发生变化,没有响应式

六.customref

作用:创建一个自定义的ref,并对其依赖项跟踪和更新触发进行显式控制

实现防抖效果代码实现:

  1. <template>
  2. <input type="text" v-model="keyWord" />
  3. <h3>{{ keyWord }}</h3>
  4. </template>
  5. <script>
  6. import { ref, customRef } from 'vue'
  7. import Demo from './components/Demo.vue'
  8. export default {
  9. name: 'App',
  10. setup () {
  11. // 自定义ref
  12. function myRef (value, delay) {
  13. let timer
  14. return customRef((track, trigger) => {
  15. return {
  16. get () {
  17. track() // 通知Vue追踪value的变化
  18. console.log('有人从myRef中读取数据了')
  19. return value
  20. },
  21. set (newValue) {
  22. console.log('有人把myRef中的数据改了')
  23. clearTimeout(timer)
  24. timer = setTimeout(() => {
  25. value = newValue
  26. trigger() // 通知vue去重新解析模板
  27. }, delay)
  28. }
  29. }
  30. })
  31. }
  32. let keyWord = myRef('hello', 500)
  33. return { keyWord }
  34. }
  35. }
  36. </script>

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

闽ICP备14008679号