Skip to content

useWidget

useWidget 为低代码设计器中的组件提供统一的运行时上下文封装,包括属性绑定、事件处理、插槽访问、权限控制、生命周期管理及数据求值等能力。

WidgetRenderProps 类型

typescript
// 编辑器渲染组件参数上下文
export interface WidgetRenderProps {
  editor: DesignerEditor                    // 当前设计器编辑器实例
  parentWidget?: WidgetInstance             // 父级组件实例(可选,根组件可能没有父级)
  parentRenderContext: WidgetRenderContext  // 父级组件的渲染上下文
  widget: WidgetInstance                    // 当前正在渲染的组件实例
  widgetDefine: WidgetDefine                // 当前组件的定义信息(如类型、属性配置等)
  widgetRenderContext: WidgetRenderContext  // 当前组件自身的渲染上下文
  widgetIndex?: number                      // 当前组件在其父容器中的索引位置(可选)
}

使用方法

typescript
// 定义组件 props
const props = defineProps<WidgetRenderProps>()

// 使用 useWidget
const { 解构属性/方法 } = useWidget(props)

useWidget 返回值描述

属性/方法名类型说明
editorDesignerEditor当前设计器编辑器实例。
parentWidgetWidgetInstance | undefined父级组件实例(可选,根组件可能没有父级)。
parentRenderContextWidgetRenderContext父级组件的渲染上下文。
widgetWidgetInstance当前正在渲染的组件实例。
widgetDefineWidgetDefine当前组件的定义信息(如类型、属性配置等)。
widgetRenderContextWidgetRenderContext当前组件自身的渲染上下文。
widgetIndexnumber | undefined当前组件在其父容器中的索引位置。
widgetPropsComputedRef<Record<string, any>>当前组件实例属性的配置值。
widgetPropsBindComputedRef<Record<string, any>>当前组件实例属性的绑定配置。
storeDesignerStore(设计器 Store 实例)当前编辑器状态管理实例。
isPreviewModeboolean是否处于预览模式。
evalFnContextEvalFnContext可执行函数所用的上下文对象。
isShowAsyncComputedRef<boolean>异步计算结果,表示组件是否应显示(结合权限(_vIfPermis)与自定义函数(_vIfFun))。
getEmitter() => EventEmitter获取事件总线实例,用于在页面中发布自定义事件。
seekData(bindConfig, defaultValue) => any根据绑定配置从上下文中解析实际数据值。
usePropValue(key: string) => any获取指定 key 的属性值(支持静态值或绑定值)。
usePropObject(...keys: string[]) => Record<string, any>批量获取多个属性值,返回键值对对象。
usePropAndEvent(args?: { only?: string[]; omit?: string[] }) => Record<string, any>合并返回符合条件的属性和事件处理器(事件名自动转为 onXxx 格式),返回键值对对象。
exposeContext(ctx: Record<string, any>) => void暴露当前组件的上下文到全局,以便其他组件或在可执行函数中使用。
useExposeContext(key?: any) => (Record<string, any> | undefined)根据指定 组件_vid _var 从组件的上下文, 默认获取当前组件上下文。
useParent(args: { _vid?: string, _var?: string }) => WidgetInstance | undefined查找满足条件的父组件实例。
useSlot(key: string | number) => WidgetInstance获取指定名称或索引的插槽组件实例。
useDefaultSlot() => WidgetInstance获取默认插槽(slotKey === 'default')的组件实例。
useSlotObject(...keys: string[]) => Record<string, WidgetInstance>批量获取指定名称的插槽组件,返回键值对对象。
useEventBind(key: string) => DesignerEditorEvalFunction | undefined获取组件事件绑定的可执行函数配置。
toEvalFunction(bind:DesignerEditorEvalFunction) => Function | undefined将可执行函数配置包装为可执行函数。
useIsShowFunction(obj?: { _vIfPermis?: any; _vIfFun?: any }) => (...args: any[]) => Promise<boolean>_vIfPermis_vIfFun 配置转换为一个异步判断是否显示的函数,融合权限校验与自定义逻辑。
useVForItemRefData() => any如果当前组件在 v-for 循环上下文中则获取当前 v-for 循环项的数据引用。
toActionButtonProps(item?: ActionButtonConfig | ActionButtonConfig[]) => ActionButtonProps | ActionButtonProps[] | undefined将按钮配置转换为带权限控制和回调处理的。
refresh() => void触发当前组件刷新。