【发布时间】:2023-01-29 20:57:33
【问题描述】:
我正在构建可重用的 UI 控件,这些控件遵循 FormLayoutComponent 中指定的特定布局和功能(标签、编辑/确认按钮、读/写视图等)。
我目前必须在每个 UI 组件中指定 FormLayoutComponent 的输入/输出。
如果我需要向 FormLayoutComponent 添加新的输入/输出,我将不得不遍历每个 UI 组件并更新它们。
我可以让 FormLayoutComponent 和 UI 组件扩展一个抽象类。这会处理打字稿类,但如果添加新属性,html 模板仍需要更新。
export abstract class AbstractLayout<T = any> {
@Input() control: FormControl<T | null> = new FormControl<T | null>(null);
@Output() confirm: EventEmitter<T | null> = new EventEmitter<T | null>();
@Input() defaultMode: DefaultMode = 'read';
@Input() showEditButton = true;
@Input() editButtonIcon = 'edit';
@Input() showConfirmButton = true;
@Input() showCancelButton = true;
@Input() showLabel = true;
@Input() requireConfirmation = true;
@Input() label: string = '';
}
在 Angular 中是否有更好的方法来实现这一点,或者当新的输入/输出添加到 FormLayoutComponent 时我是否必须更新每个 UI 组件?
Stackblitz 示例:https://stackblitz.com/edit/angular-layout-composition
【问题讨论】:
标签: angular typescript