简体   繁体   English

带有对象文字的Angular2初始化抽象类

[英]Angular2 init abstract class with object literal

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

For example doing this only returns a object with those defined properties. 例如,执行此操作仅返回具有那些已定义属性的对象。

private gridConf: GridColumn = <GridColumn>{
    field: "test2",
    header: "Test2",
    filter: true,
    filterMatchMode: "contains",
    filterPlaceholder: "Search from Test",
    sortable: true,
    selectionMode: "single"
};

What i want is a object of type GridColumn That has all the defined properties and all the default values. 我想要的是GridColumn类型的对象,它具有所有定义的属性和所有默认值。
This does not work: 这不起作用:

private gridConf: GridColumn = GridColumn({
    field: "test2",
    header: "Test2",
    filter: true,
    filterMatchMode: "contains",
    filterPlaceholder: "Search from Test",
    sortable: true,
    selectionMode: "single"
});

Constructor would force me to write a long one and i would always have to add all properties in a specific order. 构造函数会迫使我写一个很长的代码,而我总是必须按特定顺序添加所有属性。

The end goal would be to use something like this that has all default and/or defined properties in any order: 最终目标将是使用具有以下所有顺序的默认和/或定义属性的类似内容:

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"
    }
];

Closest "hack" i could find was to remove abstract and add, where fields refers to itself: 我能找到的最接近的“ hack”是删除摘要并添加,其中字段引用自身:

public constructor(
    fields?: GridColumn) {
    if (fields) Object.assign(this, fields);
}

This is not really possible, but you could find a hacky way around it I suppose. 这实际上是不可能的,但是我想您可以找到一种解决方法。 Not saying this is best practice, but if you really really want it, this is how you can do it. 并不是说这是最佳做法,但是如果您真的想要它,这就是您可以做到的方式。 FYI, default values are only initialised if you use new GridColumn() on a non abstract class: 仅供参考,仅当在非抽象类上使用new GridColumn() ,才会初始化默认值:

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

Usage 用法

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

Let me again stress that I believe that this is not best practice. 让我再次强调,我认为这不是最佳做法。 Even naming an interface starting with an I is frowned upon :D 甚至以I开头的接口都被命名为:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM