什么是hook.
本质上hook是一个函数,把setup函数中使用的composition API(ref函数,reactive函数, computed, watch,watchEffect, 生命周期(onBeforeCreate ,onCreated, onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmounted, onUnmounted,))进行了封装。
类似于vue2.X中的mixin
自定义hook的优势是,利用代码, 让setup中的逻辑更清楚易懂。
案例封装了一个js文件
使用:
import usePoint from ‘…/hook/usePoint’
setup(){
let point = usePoint()
return {point}
}