在Angular doc's cookbook for dynamic forms中,建议使用此问题的基类(它们的表单元素之一:
export class QuestionBase<T> {
value: T;
key: string;
label: string;
required: boolean;
order: number;
controlType: string;
type: string;
options: {key: string, value: string}[];
constructor(options: {
value?: T,
key?: string,
label?: string,
required?: boolean,
order?: number,
controlType?: string,
type?: string
} = {}) {
this.value = options.value;
this.key = options.key || '';
this.label = options.label || '';
this.required = !!options.required;
this.order = options.order === undefined ? 1 : options.order;
this.controlType = options.controlType || '';
this.type = options.type || '';
}
}
并且在使用它时,会传入一个类型数据
export class TextboxQuestion extends QuestionBase<string> {/* input-type specific code */}
^^^^^^
export class DropdownQuestion extends QuestionBase<string> {/* input-type specific code */}
^^^^^^
我不明白您为什么要将此类型传递给QuestionBase
类。广义地说,将类型传递给这样的类为什么有用?在这个特定的示例中,为什么要传递可选属性的类型(不确定它是否是可选属性),此外,对于HTML输入,据我所知,它们都返回字符串,所以目的是什么?] >
在Angular doc的动态表单食谱中,建议使用此问题的基类(它们的表单元素之一):导出类QuestionBase
这是在TypeScript中编写泛型的方式