簡體   English   中英

Angular2 和 TypeScript 枚舉——該做的和不該做的

[英]Angular2 and TypeScript enums - dos and don'ts

我的 Angular2 應用程序中有一堆枚舉,對於每個枚舉,我都創建了一個相應的 utils 類,該類提供了一些附加功能。 這只是繞過了 TypeScript 的限制,即我無法像在 Java 中那樣向枚舉添加函數。

這是我的國家/地區枚舉和相應的 utils 類的示例:

import {Injectable} from "@angular/core";

export enum Country {
    ANDORRA = <any>"ANDORRA",
    UNITED_ARAB_EMIRATES = <any>"UNITED_ARAB_EMIRATES",
    AFGHANISTAN = <any>"AFGHANISTAN",
    // ...
    // more countries skipped for brevity 
    // ...
    ZAMBIA = <any>"ZAMBIA",
    ZIMBABWE = <any>"ZIMBABWE"
}


@Injectable()
export class CountryUtils {

    private _emptyEntry: Array<string> = ["", "", ""];

    private countryData: any = {
        ANDORRA: ["Andorra", "AD", "AND"],
        UNITED_ARAB_EMIRATES: ["United Arab Emirates", "AE", "ARE"],
        AFGHANISTAN: ["Afghanistan", "AF", "AFG"],
        // ...
        // more countries skipped for brevity
        // ...
        ZAMBIA: ["Zambia", "ZM", "ZMB"],
        ZIMBABWE: ["Zimbabwe", "ZW", "ZWE"]
    }

    getData(country: Country): any {
        if (!country) {
            return this._emptyEntry;
        }
        return this.countryData[Country[country]];
    }

    getName(country: Country): any {
        return this.getData(country)[0];
    }

    getCode2(country: Country): any {
        return this.getData(country)[1];
    }

    getCode3(country: Country): any {
        return this.getData(country)[2];
    }
}

現在,如您所見,我已將 utils 類標記為 Injectable,以便我可以在需要的地方將它與 DI 一起使用。

我面臨的問題是,這些枚舉也用於不依賴於 Angular2 的核心 TS/JS 模型類中,因此我無法使用 DI 在核心內注入我的 utils 類的實例(如上面的那個)模型類。

我看到的唯一解決方法是刪除Injectable()方法並將 utils 類中的方法標記為靜態。 因此,通過這種方式,我可以將每個 utils 類與枚舉一起導入,並在我想要的任何地方使用它。

我的問題是,設計決策會有多糟糕? 在 Angular2 中做這樣的事情是否可以接受,或者完全禁止使用靜態? 與常規 DI 方法相比,我看不到任何特別有害的東西,除了它就像眼痛。

任何建議將不勝感激。 提前致謝!

我不認為靜態類有問題。 此外,您可以使用聲明合並將您的 util 功能添加到您的枚舉中。 不需要額外的課程。 檢查下面。

enum Country {
    ANDORRA = <any>"ANDORRA",
    UNITED_ARAB_EMIRATES = <any>"UNITED_ARAB_EMIRATES",
    AFGHANISTAN = <any>"AFGHANISTAN",
    // ...
    // more countries skipped for brevity 
    // ...
    ZAMBIA = <any>"ZAMBIA",
    ZIMBABWE = <any>"ZIMBABWE"
}

namespace Country {

    const _emptyEntry: Array<string> = ["", "", ""];

    const countryData: any = {
        ANDORRA: ["Andorra", "AD", "AND"],
        UNITED_ARAB_EMIRATES: ["United Arab Emirates", "AE", "ARE"],
        AFGHANISTAN: ["Afghanistan", "AF", "AFG"],
        // ...
        // more countries skipped for brevity
        // ...
        ZAMBIA: ["Zambia", "ZM", "ZMB"],
        ZIMBABWE: ["Zimbabwe", "ZW", "ZWE"]
    };

    export function getData(country: Country): any {
        if (!country) {
            return this._emptyEntry;
        }
        return this.countryData[Country[country]];
    }

    export function getName(country: Country): any {
        return this.getData(country)[0];
    }

    export function getCode2(country: Country): any {
        return this.getData(country)[1];
    }

    export function getCode3(country: Country): any {
        return this.getData(country)[2];
    }
}

Country.getCode2(Country.AFGHANISTAN);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM