组合函数就是将逻辑抽离成独立函数,供多个组件复用。
典型特征:
- 命名以 use 开头
- 内部使用 ref/reactive 管理响应式状态
- 可返回状态、方法、生命周期逻辑
封装模板:
ts
复制编辑
export function useCounter(init = 0) {
const count = ref(init)
const inc = () => count.value++
const reset = () => (count.value = init)
return { count, inc, reset }
}
推荐使用场景:
- 表单处理逻辑
- 本地缓存逻辑(如草稿)
- 页面切换控制、滚动事件处理
https://www.sohu.com/a/910454515_122459568
https://www.sohu.com/a/910452653_122459568
https://www.sohu.com/a/910451737_122459568
https://www.sohu.com/a/910448988_122459568
https://www.sohu.com/a/910448961_122459568
https://www.sohu.com/a/910458110_122459559
https://www.sohu.com/a/910457842_122459559
https://www.sohu.com/a/910457817_122459559
https://www.sohu.com/a/910457046_122459559
https://www.sohu.com/a/910458790_122459428
https://www.sohu.com/a/910459091_122459428
https://www.sohu.com/a/910459366_122459428
https://www.sohu.com/a/910459848_122459428
https://www.sohu.com/a/910460703_122459428
https://www.163.com/dy/article/K3J53OBJ0556EC6N.html
https://www.163.com/dy/article/K3J57DDV0556EC6N.html
https://blog.csdn.net/gregg4265117/article/details/149105176
https://blog.csdn.net/gregg4265117/article/details/149105010
https://blog.csdn.net/gregg4265117/article/details/149105232
https://blog.csdn.net/gregg4265117/article/details/149105255
https://blog.csdn.net/gregg4265117/article/details/149105276