带有对象文字的Angular2 init抽象类
例如,执行此操作仅返回具有这些已定义属性的对象带有对象文字的Angular2 init抽象类,angular,typescript,Angular,Typescript,例如,执行此操作仅返回具有这些已定义属性的对象 export abstract class GridColumn { public field?: string; public sortField?: string; public header?: string; public footer?: string; public sortable?: any = true; public editable?: boolean = false;
export abstract class GridColumn {
public field?: string;
public sortField?: string;
public header?: string;
public footer?: string;
public sortable?: any = true;
public editable?: boolean = false;
public filter?: boolean = true;
public filterMatchMode?: string = 'contains';
public filterPlaceholder?: string;
public style?: any;
public styleClass?: string;
public hidden?: boolean = false;
public selectionMode?: string = 'multiple';
public frozen?: boolean;
}
构造函数会迫使我写一个长的,而且我总是必须按特定顺序添加所有属性
最终目标是使用这样的东西,它以任何顺序具有所有默认和/或定义的属性:
private gridConf: GridColumn = GridColumn({
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
});
这是不可能的,但我想你可以找到一个简单的方法。不是说这是最佳实践,但如果你真的想要,这就是你可以做到的。仅供参考,只有在非抽象类上使用
new GridColumn()
时,才会初始化默认值:
GridColumn
public constructor(
fields?: GridColumn) {
if (fields) Object.assign(this, fields);
}
IGridColumn
export class GridColumn implements IGridColumn {
public sortable: boolean = true;
public editable: boolean = false;
public filter: boolean = true;
public filterMatchMode: string = 'contains';
public hidden: boolean = false;
public selectionMode: string = 'multiple';
public field: string;
public sortField: string;
public header: string;
public footer: string;
public filterPlaceholder: string;
public style: any;
public styleClass: string;
public frozen: boolean;
constructor(data: IGridColumn = {}){
Object.assign(this, data);
}
}
用法
export interface IGridColumn {
public field?: string;
public sortField?: string;
public header?: string;
public footer?: string;
public sortable?: boolean;
public editable?: boolean;
public filter?: boolean;
public filterMatchMode?: string;
public filterPlaceholder?: string;
public style?: any;
public styleClass?: string;
public hidden?: boolean;
public selectionMode?: string;
public frozen?: boolean;
}
专用列:数组=[
新网格列({
字段:“测试”,
选择模式:“单一”,
过滤器:对,
filterMatchMode:“包含”,
filterPlaceholder:“从测试中搜索”,
可排序:是的,
标题:“测试”
}),
新网格列({
字段:“test2”,
标题:“Test2”,
过滤器:对,
filterMatchMode:“包含”,
filterPlaceholder:“从测试中搜索”,
可排序:是的,
选择模式:“单一”
})
];
让我再次强调,我认为这不是最佳做法。甚至连以I开头的接口命名都不受欢迎:D
export class GridColumn implements IGridColumn {
public sortable: boolean = true;
public editable: boolean = false;
public filter: boolean = true;
public filterMatchMode: string = 'contains';
public hidden: boolean = false;
public selectionMode: string = 'multiple';
public field: string;
public sortField: string;
public header: string;
public footer: string;
public filterPlaceholder: string;
public style: any;
public styleClass: string;
public frozen: boolean;
constructor(data: IGridColumn = {}){
Object.assign(this, data);
}
}
export interface IGridColumn {
public field?: string;
public sortField?: string;
public header?: string;
public footer?: string;
public sortable?: boolean;
public editable?: boolean;
public filter?: boolean;
public filterMatchMode?: string;
public filterPlaceholder?: string;
public style?: any;
public styleClass?: string;
public hidden?: boolean;
public selectionMode?: string;
public frozen?: boolean;
}
private columns: Array<GridColumn> = [
new GridColumn(<IGridColumn>{
field: "test",
selectionMode: "single",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
header: "Test"
}),
new GridColumn(<IGridColumn>{
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
})
];