2017-02-27 79 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