Skip to content

Hooks 工具函数

SeeYouUI 提供了一组实用的 Hooks,用于处理路由、安全区、滚动监听、树形数据等常见场景。

useRoute

跨平台路由统一封装,统一参数格式,自动编码 params。

typescript
import { useRoute } from '@/uni_modules/see-u-ui/utils/hooks/useRoute'

const { navigateTo, redirectTo, switchTab, reLaunch, navigateBack, currentRoute, getQuery } = useRoute()

返回值

属性/方法类型说明
navigateTo(options: RouteOptions) => Promise<void>跳转到新页面(压栈)
redirectTo(options: RouteOptions) => Promise<void>重定向到新页面(替换当前页)
switchTab(options: RouteOptions) => Promise<void>切换 Tab 页
reLaunch(options: RouteOptions) => Promise<void>关闭所有页面并打开新页面
navigateBack(delta?: number) => Promise<void>返回上一页,默认 delta=1
currentRouteRef<{ path, params }>当前路由信息
getQuery<T>() => T获取当前页面参数

RouteOptions

属性类型说明
urlstring目标页面路径
paramsRecord<string, any>路由参数,自动序列化编码
animationTypestring页面动画类型(仅 APP)

示例

typescript
// 跳转并传递参数
await navigateTo({
  url: '/pages/detail/index',
  params: { id: '123', name: 'test' }
})

// 返回上一页
await navigateBack()

// 返回上两页
await navigateBack(2)

useSafeArea

获取设备安全区内边距信息,结果缓存在模块级别(单例模式)。

typescript
import { useSafeArea } from '@/uni_modules/see-u-ui/utils/hooks/useSafeArea'

const { top, bottom, left, right } = useSafeArea()

返回值

属性类型说明
topRef<number>顶部安全区距离(px)
bottomRef<number>底部安全区距离(px)
leftRef<number>左侧安全区距离(px)
rightRef<number>右侧安全区距离(px)

示例

vue
<template>
  <view :style="{ paddingTop: top + 'px' }">
    <text>内容区域</text>
  </view>
</template>

<script setup>
import { useSafeArea } from '@/uni_modules/see-u-ui/utils/hooks/useSafeArea'
const { top, bottom } = useSafeArea()
</script>

useScrollSpy

监听页面或指定容器的滚动位置,支持节流和方向判断。

typescript
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'

const { scrollTop, isScrolling, scrollDirection } = useScrollSpy()

参数

属性类型默认值说明
targetstring''滚动容器选择器(H5 专用);缺省时监听 window/页面滚动
throttlenumber0节流间隔(ms)。0(默认)使用 requestAnimationFrame 节流;> 0 使用 setTimeout 节流(leading + trailing)

返回值

属性类型说明
scrollTopRef<number>当前滚动距离(px)
isScrollingRef<boolean>是否正在滚动
scrollDirectionRef<'up' | 'down' | 'idle'>滚动方向,初始为 'idle'

示例

vue
<template>
  <view v-if="scrollTop > 200" class="back-top" @tap="scrollToTop">
    回到顶部
  </view>
</template>

<script setup>
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop, scrollDirection } = useScrollSpy()
</script>

useTree

树形数据管理 Hook,提供扁平化、展开/折叠、选中/级联、搜索过滤、懒加载等功能。

typescript
import { useTree } from '@/uni_modules/see-u-ui/utils/hooks/useTree'

const {
  flatNodes, expandedKeys, checkedKeys,
  toggleExpand, toggleCheck, expandAll, collapseAll,
  filterNodes, getCheckedNodes, appendNode, removeNode
} = useTree({
  data: treeData,
  keyField: 'id',
  labelField: 'label',
  childrenField: 'children',
  selectMode: 'check-cascade'
})

参数

属性类型默认值说明
dataRef<TreeNode[]>-树形数据(响应式)
keyFieldstring'id'主键字段名
labelFieldstring'label'标签字段名
childrenFieldstring'children'子节点字段名
selectModestring'single'选择模式:single / multiple / check-cascade
isLazybooleanfalse是否启用懒加载

TreeNode

属性类型说明
idstring | number节点唯一标识
labelstring节点标签
childrenTreeNode[]子节点列表
isDisabledboolean是否禁用
isLeafboolean是否为叶子节点

返回值

属性/方法类型说明
flatNodesComputedRef<FlatNode[]>扁平化后的节点列表
expandedKeysRef<Set>展开的节点 key 集合
checkedKeysRef<Set>选中的节点 key 集合
toggleExpand(key) => void切换展开/折叠
toggleCheck(key) => void切换选中状态
expandAll() => void展开所有节点
collapseAll() => void折叠所有节点
filterNodes(query) => void搜索过滤
getCheckedNodes() => TreeNode[]获取选中的节点
appendNode(parentKey, node) => void添加节点
removeNode(key) => void移除节点
updateNode(key, data) => void更新节点

示例

typescript
import { ref } from 'vue'
import { useTree } from '@/uni_modules/see-u-ui/utils/hooks/useTree'

const treeData = ref([
  {
    id: 1,
    label: '节点1',
    children: [
      { id: 11, label: '节点1-1' },
      { id: 12, label: '节点1-2' }
    ]
  }
])

const { flatNodes, toggleExpand, expandAll, getCheckedNodes } = useTree({
  data: treeData,
  selectMode: 'check-cascade'
})

辽 ICP 备 2025070134 号