0
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;
}
例如,這樣做只會返回一個具有這些定義屬性的對象。Angular2 init抽象類與對象文字
private gridConf: GridColumn = <GridColumn>{
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
};
我要的是GridColumn
具有所有定義的屬性和所有默認值類型的對象。
這不起作用:
private gridConf: GridColumn = GridColumn({
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
});
構造會強迫我寫一個漫長的,我會一直都在一個特定的順序添加的所有屬性。
的最終目標是使用這樣的事情有任何順序所有默認和/或定義的屬性:
private columns: Array<GridColumn> = [
<GridColumn>{
field: "test",
selectionMode: "single",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
header: "Test"
},
<GridColumn>{
field: "test2",
header: "Test2",
filter: true,
filterMatchMode: "contains",
filterPlaceholder: "Search from Test",
sortable: true,
selectionMode: "single"
}
];
最近的「黑客」我能找到的是去除抽象和添加,其中場指的是它本身:
public constructor(
fields?: GridColumn) {
if (fields) Object.assign(this, fields);
}