Wrappers — 包装器设置
本文介绍 Wrapper 的 Actions(set/patch/remove/patchAsync)以及编写自定义 Wrapper 组件的方法。
actions.wrappers — 包装器操作
Section titled “actions.wrappers — 包装器操作”set — 设置包装器列表
Section titled “set — 设置包装器列表”将指定包装器列表设置为当前字段的 wrappers(覆盖已有值):
patch — 追加包装器(不清空已有)
Section titled “patch — 追加包装器(不清空已有)”set 会先清空再设置;patch 则在已有列表末尾追加:
patchAsync — 异步添加包装器
Section titled “patchAsync — 异步添加包装器”将新包装器添加到列表末尾,或插入到指定位置:
changeAsync — 修改已有包装器
Section titled “changeAsync — 修改已有包装器”接收一个定位函数(参数为当前 wrappers 的输入 Signal 列表,返回目标 wrapper 的数据源)和要应用的 actions:
remove — 移除包装器
Section titled “remove — 移除包装器”Wrapper 渲染顺序
Section titled “Wrapper 渲染顺序”Wrapper 从外到内依次包裹字段:
每个 Wrapper 组件负责渲染其内部内容(通过 InsertFieldDirective)。
编写自定义 Wrapper 组件
Section titled “编写自定义 Wrapper 组件”Wrapper 要求
Section titled “Wrapper 要求”一个有效的 Wrapper 组件需要:
- 不继承任何基类 — Wrapper 是纯装饰器,与 Group 组件(继承
PiyingViewGroupBase)不同 - 导入
InsertFieldDirective— 在imports数组中注册 - 使用
<ng-container insertField>— 作为字段插入点 - V2 模式用
<ng-template #templateRef>包裹模板 — 通过viewChild获取引用
V2 Wrapper 模板(推荐)
Section titled “V2 Wrapper 模板(推荐)”V2 模式下,整个包装器的 UI 结构放在 <ng-template #templateRef> 中:
对应的模板文件 card.wrapper.html:
V1 Wrapper 模板(兼容)
Section titled “V1 Wrapper 模板(兼容)”V1 模式下,不使用 <ng-template #templateRef> 包裹:
Wrapper 的 Inputs
Section titled “Wrapper 的 Inputs”Wrapper 组件通过 Angular @Input() 接收配置值(见下方 Wrapper 组件的 Inputs/Attributes 章节)。
V1 与 V2 的区别
Section titled “V1 与 V2 的区别”| 特性 | V1(兼容) | V2(推荐) |
|---|---|---|
templateRef 声明 |
不需要 | <ng-template #templateRef> 包裹整个 UI |
| TypeScript | 不需要设置 | templateRef = viewChild.required('templateRef') |
| 版本标记 | 不设置 __version |
static __version = 2 |
<ng-container insertField> |
直接写在模板顶层 | 放在 <ng-template #templateRef> 内部 |
核心相同点:无论 V1 还是 V2,字段插入点都是
<ng-container insertField>,这是 Wrapper 的唯一职责。
Wrapper 配置
Section titled “Wrapper 配置”在 fieldGlobalConfig.wrappers 中注册
Section titled “在 fieldGlobalConfig.wrappers 中注册”Wrapper 默认 Actions
Section titled “Wrapper 默认 Actions”可以在 fieldGlobalConfig.wrappers 中为包装器设置默认 Actions:
Wrapper 组件的 Inputs/Attributes
Section titled “Wrapper 组件的 Inputs/Attributes”Wrapper 同样支持 actions.wrappers 的对象格式指定 inputs:
Wrapper 组件通过 Angular @Input() 接收这些值。
Wrapper 编写注意事项
Section titled “Wrapper 编写注意事项”| 要点 | 说明 |
|---|---|
| 不继承任何基类 | Wrapper 是纯装饰器,与 Group(PiyingViewGroupBase)不同 |
imports: [InsertFieldDirective] |
必须导入 InsertFieldDirective,否则 insertField 指令不可用 |
V2 使用 <ng-template #templateRef> 包裹整个 UI |
通过 viewChild.required('templateRef') 获取引用 |
V1 直接写 <ng-container insertField> |
V1 模板中不需要 templateRef |
V2 设置 static __version = 2 |
标记使用 V2 模板语法 |
<ng-container insertField> 是字段插入点 |
不可省略,V1/V2 完全相同 |
| Wrapper 之间可以嵌套 | 一个 Wrapper 内再套另一个 Wrapper |
- API: path-querying — fullPath / keyPath / get() / @alias
- API: fieldGlobalConfig — types/wrappers 全局配置优先级体系
- formConfig — disabled / emptyValue / deletionMode 等配置详解