Vue3 Hooks:从原理到实战封装指南

一、Hooks 的定义与核心价值

在 Vue3 的 Composition API 体系中,Hooks(组合式函数) 是通过封装响应式逻辑来实现代码复用的核心方案。其核心思想借鉴 React Hooks,但结合 Vue 的响应式系统形成了独特的实现方式。

与传统方案的对比:

方案优点缺点
Mixins逻辑复用命名冲突、来源不明确、难以追踪
工具函数纯函数无副作用无法使用响应式特性
Hooks响应式支持、逻辑组合、作用域隔离需要理解响应式原理

二、Hooks 与普通函数的本质区别
  1. 响应式能力
// Hooks 内部使用响应式 API
import { ref, onMounted } from 'vue'

export function useCounter(initialValue = 0) {
  const count = ref(initialValue)
  
  const increment = () => count.value++
  
  return { count, increment }
}
  1. 生命周期集成
function useMouse() {
  const x = ref(0)
  const y = ref(0)

  const update = e => {
    x.value = e.pageX
    y.value = e.pageY
  }

  onMounted(() => window.addEventListener('mousemove', update))
  onUnmounted(() => window.removeEventListener('mousemove', update))

  return { x, y }
}
  1. 上下文感知
// 普通函数无法访问组件上下文
function commonFn() {
  // 无法访问 this.$route 等实例属性
}

// Hooks 可通过组合式 API 获取上下文
import { getCurrentInstance } from 'vue'

function useRouter() {
  const { proxy } = getCurrentInstance()
  return proxy.$router
}

三、高质量 Hooks 封装原则
  1. 单一职责模式
// Bad: 混杂多种功能
function useUser() {
  // 用户数据、权限、配置混杂
}

// Good: 拆分独立 Hooks
function useUserProfile() {...}
function useUserPermissions() {...}
  1. 灵活配置设计
function usePagination(api, options = {}) {
  const {
    pageSize = 10,
    immediate = true,
    formatter = data => data
  } = options
  // ...
}
  1. 副作用管理
function useEventListener(target, event, callback) {
  onMounted(() => target.addEventListener(event, callback))
  onUnmounted(() => target.removeEventListener(event, callback))
}
  1. TypeScript 强化
interface DarkModeOptions {
  storageKey?: string
  defaultState?: boolean
}

export function useDarkMode(
  options: DarkModeOptions = {}
): { isDark: Ref<boolean>; toggle: () => void } {
  // ...
}

四、企业级常用 Hooks 实现
  1. 智能请求 Hook
export function useRequest(api, config = {}) {
  const loading = ref(false)
  const data = ref(null)
  const error = ref(null)

  const execute = async params => {
    try {
      loading.value = true
      const res = await api(params)
      data.value = config.format?.(res) || res
    } catch (err) {
      error.value = err
    } finally {
      loading.value = false
    }
  }

  return { loading, data, error, execute }
}
  1. 本地存储同步 Hook
export function useLocalStorage(key, defaultValue) {
  const state = ref(defaultValue)

  const read = () => {
    const value = localStorage.getItem(key)
    if (value !== null) state.value = JSON.parse(value)
  }

  const write = () => {
    localStorage.setItem(key, JSON.stringify(state.value))
  }

  read() // 初始化读取

  watch(state, write, { deep: true })

  return state
}
  1. 响应式视口尺寸跟踪
export function useViewport() {
  const width = ref(window.innerWidth)
  const height = ref(window.innerHeight)

  const update = () => {
    width.value = window.innerWidth
    height.value = window.innerHeight
  }

  useEventListener(window, 'resize', update)

  return { width, height }
}
  1. 智能滚动 Hook
export function useScroll(refEl) {
  const x = ref(0)
  const y = ref(0)
  const isBottom = ref(false)

  const el = refEl || window

  const handler = () => {
    if (el === window) {
      x.value = window.scrollX
      y.value = window.scrollY
      isBottom.value = 
        window.innerHeight + window.scrollY >= document.body.offsetHeight
    } else {
      x.value = el.value.scrollLeft
      y.value = el.value.scrollTop
      isBottom.value = 
        el.value.scrollHeight <= el.value.clientHeight + el.value.scrollTop
    }
  }

  useEventListener(el, 'scroll', handler)
  handler() // 初始触发

  return { x, y, isBottom }
}

五、高级技巧与最佳实践
  1. Hooks 组合
function useUserDashboard() {
  const { user } = useAuth()
  const { data: projects } = useProjectList(user.value.id)
  const { data: tasks } = useTaskList(user.value.id)

  return { user, projects, tasks }
}
  1. 性能优化
function useHeavyCalculation(data) {
  const result = computed(() => {
    // 复杂计算使用 computed 缓存
    return heavyOperation(data.value)
  })

  return result
}
  1. SSR 兼容
import { onServerPrefetch } from 'vue'

function useSSRData() {
  const data = ref(null)

  const fetchData = async () => {
    data.value = await fetch('/api/data')
  }

  onServerPrefetch(fetchData)
  onMounted(!data.value && fetchData)

  return data
}

六、总结与建议

何时使用 Hooks:

  • 需要跨组件复用的逻辑
  • 复杂组件的逻辑拆分
  • 需要响应式状态管理的工具函数

通过合理使用 Hooks,开发者可以构建出高内聚、低耦合的前端应用架构。建议在项目中建立 src/hooks 目录进行分类管理,结合 TypeScript 和单元测试构建企业级 Hook 库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钟H呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值