Vue 包 API 参考(@piying/view-vue)
本文介绍 Vue 包 @piying/view-vue 的公开 API。Vue 2 使用 @piying/view-vue2-legacy,两者 API 结构一致。
PiyingView
Section titled “PiyingView”| Props | 类型 | 说明 |
|---|---|---|
schema |
v.BaseSchema |
Valibot Schema |
modelValue |
any |
v-model 双向绑定 |
options |
FieldConvertViewOptions |
转换选项 |
PiyingFieldTemplate
Section titled “PiyingFieldTemplate”🧭 手动模式:属于 两种使用模式 中的模式二(手动绑定)。
PiyingView是自动模式入口,而PiyingFieldTemplate/PiyingField/convertToField是手动模式的手动绑定工具。
渲染字段(组件 + 包装器链 + 递归子字段),通过 field / path 定位,渲染到指定位置:
| Props | 类型 | 说明 |
|---|---|---|
field |
PiResolvedViewFieldConfig(必填) |
要渲染的字段配置 |
path |
KeyPath(可选) |
定位子字段;不传则渲染整个根字段 |
懒加载内置支持(
defineAsyncComponent+getLazyImport)。完整渲染管线与常见坑见 PiyingFieldTemplate(字段渲染)。
PiyingViewGroup
Section titled “PiyingViewGroup”字段组容器,用于渲染 object / array / record:
PiyingField
Section titled “PiyingField”🧭 手动模式:同
PiyingFieldTemplate,属于模式二(手动绑定)。
字段绑定,将字段绑定为表单控件并暴露 cvaa:
⚠️ Vue 的
cvaa.value是ShallowRef,插槽里不会自动解包,需要unref()。
| Props | 类型 | 说明 |
|---|---|---|
field |
PiResolvedViewFieldConfig(必填) |
字段配置 |
path |
KeyPath(可选) |
定位叶子子字段再绑定 |
| 默认插槽 | { cvaa, field } |
渲染作用域,类型跟着 path 推导 |
完整说明(含
cvaa成员、错误码、与PiyingFieldTemplate对比)见 PiyingField(字段绑定)。
PI_VIEW_FIELD_TOKEN— 当前字段配置(ComputedRef<PiResolvedViewFieldConfig>)InjectorToken— 静态注入器(ComputedRef<Injector>)
useControlValueAccessor — CVA 适配器
Section titled “useControlValueAccessor — CVA 适配器”返回 cva(ControlValueAccessor)和 cvaa(适配器):
cvaa 提供:
| 成员 | 类型 | 说明 |
|---|---|---|
value |
ShallowRef |
当前值 |
disabled |
Ref<boolean> |
禁用状态 |
valueChange(v) |
(v) => void |
更新值并触发变更 |
touchedChange() |
() => void |
触发 touched 回调 |
signalToRef — Signal 转 Ref
Section titled “signalToRef — Signal 转 Ref”typedComponent — 强类型 setComponent
Section titled “typedComponent — 强类型 setComponent”详见 Vue 强类型组件。
typedFieldComponentPipe — 路径 + 组件双强类型
Section titled “typedFieldComponentPipe — 路径 + 组件双强类型”按「路径 + 组件」写配置,inputs / outputs 的 key 与值类型跟着 Vue 组件走。详见 typedFieldComponentPipe(Vue 3)。
Vue 版没有
d.models:v-model就是 prop +update:xxx的约定,运行时不消费field.models。
convertToField — Schema 转换
Section titled “convertToField — Schema 转换”🧭 手动模式:
convertToField是手动模式的核心入口,返回field后需用PiyingField/PiyingFieldTemplate手动绑定渲染位置。自动模式中PiyingView会在内部自动调用它,无需手动使用。详见 两种使用模式。
rawConfig — 原生配置 Action
Section titled “rawConfig — 原生配置 Action”rawConfig 与 actions、setComponent 等 Action 一样,逻辑定义在所有框架中相同。Vue 包不单独导出这些 Action,需从框架无关的核心包导入:
注意:
@piying/view-vue包入口不导出rawConfig/actions/setComponent,请从@piying/view-core导入(React / Solid / Svelte 适配包同理)。
VueSchemaHandle
Section titled “VueSchemaHandle”Vue 的 Schema 处理句柄(继承 CoreSchemaHandle):
VueFormBuilder
Section titled “VueFormBuilder”Vue 的 FormBuilder(继承 FormBuilder<VueSchemaHandle>):
Vue 2 Legacy 差异(@piying/view-vue2-legacy)
Section titled “Vue 2 Legacy 差异(@piying/view-vue2-legacy)”| 项 | Vue 3 | Vue 2 Legacy |
|---|---|---|
useControlValueAccessor |
useControlValueAccessor(optionalBind?) |
useControlValueAccessor(autoChange?, optionalBind?) |
| 额外导出 | — | clone(rfdc 深拷贝) |
Vue2-legacy 的 useControlValueAccessor 增加了 autoChange 参数,为 true 时自动 watch(value) 触发变更:
@piying/view-vue 导出:PiyingView、PiyingFieldTemplate、PiyingViewGroup、PiyingField(别名 PiyingFieldControlBind)、PI_VIEW_FIELD_TOKEN、InjectorToken、signalToRef、useControlValueAccessor、typedComponent、typedFieldComponentPipe、convertToField、VueSchemaHandle、VueFormBuilder、VueSchema 类型。
注意:
rawConfig/actions/setComponent等 Action 需从@piying/view-core导入,@piying/view-vue不导出。
- Vue 强类型组件 — typedComponent 详解
- typedFieldComponentPipe(Vue 3) — 路径 + 组件双强类型
- 框架差异 — Vue 的 Field Token / CVA 绑定
- 基础字段定义 — setComponent / formConfig