2017-02-27 60 views
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); 
} 

回答

1

这不是真的可能,但你可以找到一个奇怪的方式,我想。不要说这是最好的做法,但如果你确实需要它,这就是你如何做到的。

的GridColumn

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); 
    } 
} 

IGridColumn

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" 
    }) 
]; 
:仅供参考,如果你在一个非抽象类中使用 new GridColumn()默认值只初始化

让我再次强调,我认为这不是最佳实践。甚至命名以I开头的接口也是令人不悦的:D