Skip to content

Hooks API

Trix Admin 提供的组合式函数(Composables)。

useSchemaMethods

Schema 内置方法 Hook,提供可在 JSON Schema 中调用的导航、标签页、窗口等方法。

导入

typescript
import { useSchemaMethods } from '@/hooks'

用法

typescript
const { $nav, $tab, $window, schemaMethods } = useSchemaMethods()

// 在 DynamicPage 中使用
<VSchema :schema="schema" :methods="schemaMethods" />

返回值

方法参数说明
pushpath: string | object跳转到指定路径
replacepath: string | object替换当前页面(不产生历史记录)
backdelta?: number返回上一页,默认 1 步
forwarddelta?: number前进,默认 1 步

$tab - 标签页方法

方法参数说明
closetabId?: string关闭标签页,默认关闭当前标签
closeAndGopath?: string关闭当前标签并跳转到指定页面
closeOtherstabId?: string关闭其他标签页
closeLefttabId?: string关闭左侧标签页
closeRighttabId?: string关闭右侧标签页
openpath: string, title?: string新建标签页并跳转
openIframeurl: string, title: string新建 iframe 标签页
refresh-刷新当前标签页
fixtabId?: string固定标签页
unfixtabId?: string取消固定标签页
isFixedtabId?: string判断标签页是否固定

$window - 窗口方法

方法参数说明
openurl: string, target?: string, features?: string在新窗口打开 URL
close-关闭当前窗口
print-打印当前页面

在 Schema 中使用

通过 $methods 访问这些内置方法:

json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$nav.push", "args": ["/user/profile"] }
  },
  "children": "跳转到个人资料"
}
json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$tab.close" }
  },
  "children": "关闭当前标签"
}
json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$window.open", "args": ["https://example.com"] }
  },
  "children": "打开新窗口"
}

useSchemaLoader

加载 JSON Schema 的 Hook。

导入

typescript
import { useSchemaLoader } from '@/hooks'

用法

typescript
const {
  schema,
  loading,
  error,
  retryCount,
  maxRetries,
  retry
} = useSchemaLoader({
  source: '/mock/schema/page.json',
  watchSource: true,
  immediate: true,
  maxRetries: 3,
  onLoading: () => console.log('加载中'),
  onLoaded: (schema) => console.log('加载完成', schema),
  onError: (err) => console.error('加载失败', err)
})

参数

参数类型默认值说明
sourceRef<string> | string-Schema 来源 URL
watchSourcebooleanfalse是否监听 source 变化
immediatebooleantrue是否立即加载
requireSourceRef<boolean> | booleantrue是否必须有 source
missingSourceMessagestring-source 缺失时的错误信息
maxRetriesnumber3最大重试次数
initialLoadingbooleantrue初始加载状态
onLoading() => void-开始加载回调
onLoaded(schema) => void-加载完成回调
onError(error) => void-加载失败回调

返回值

属性类型说明
schemaRef<JsonNode | null>加载的 Schema
loadingRef<boolean>加载状态
errorRef<string | null>错误信息
retryCountRef<number>当前重试次数
maxRetriesnumber最大重试次数
retry() => void重试方法

useBoolean

布尔值状态管理 Hook。

导入

typescript
import { useBoolean } from '@/hooks'

用法

typescript
const { bool, setTrue, setFalse, toggle } = useBoolean(false)

// 设置为 true
setTrue()

// 设置为 false
setFalse()

// 切换
toggle()

参数

参数类型默认值说明
initialValuebooleanfalse初始值

返回值

属性类型说明
boolRef<boolean>布尔值
setTrue() => void设置为 true
setFalse() => void设置为 false
toggle() => void切换值
set(value: boolean) => void设置值

useLoading

加载状态管理 Hook。

导入

typescript
import { useLoading } from '@/hooks'

用法

typescript
const { loading, startLoading, endLoading } = useLoading()

// 开始加载
startLoading()

// 结束加载
endLoading()

// 带延迟的加载
const { loading, startLoading, endLoading } = useLoading(true, 300)

参数

参数类型默认值说明
initialValuebooleanfalse初始值
delaynumber0延迟时间(毫秒)

返回值

属性类型说明
loadingRef<boolean>加载状态
startLoading() => void开始加载
endLoading() => void结束加载

useContext

获取上下文 Hook。

导入

typescript
import { useContext } from '@/hooks'

用法

typescript
const { isMobile, locale, darkMode } = useContext()

返回值

属性类型说明
isMobileComputedRef<boolean>是否移动端
localeComputedRef<string>当前语言
darkModeComputedRef<boolean>是否深色模式

usePermission

权限检查 Hook。

导入

typescript
import { usePermission } from '@/hooks'

用法

typescript
const { hasPermission, hasAnyPermission, hasAllPermissions } = usePermission()

// 检查单个权限
if (hasPermission('user:write')) {
  // ...
}

// 检查任一权限
if (hasAnyPermission(['user:write', 'user:admin'])) {
  // ...
}

// 检查所有权限
if (hasAllPermissions(['user:read', 'user:write'])) {
  // ...
}

返回值

方法参数返回值说明
hasPermissionstringboolean检查单个权限
hasAnyPermissionstring[]boolean检查任一权限
hasAllPermissionsstring[]boolean检查所有权限

基于 MIT 许可发布