2017-08-05 51 views
1

我正在尝试使enum处理国家&货币代码。 enum必须通过整个应用程序(Ionic 3 Angular 4应用程序)使用。角4使用命名空间导出枚举

到目前为止,我发现这种方式:

enum CountryCode { 
    TH, 
    BGD, 
} 

namespace CountryCode { 
    export function getCurrencyCode(country: CountryCode) { 
    switch (country) { 
     case CountryCode.TH: 
     return 'THB'; 
     case CountryCode.BGD: 
     return 'BDT'; 
     default: 
     return 'THB'; 
    } 
    } 
} 
然而

在这种情况下,enum不能出口到其他模块。

我该如何解决这个问题?

+0

出口添加到您的枚举 - 出口枚举COUNTRYCODE {} – JEMI

回答

2

你应该声明为下面的命名空间内,

export namespace CountryCode { 
    export enum CountryCode { 
     TH, 
     BGD, 
    } 
    export function getCurrencyCode(country: CountryCode) { 
     switch (country) { 
      case CountryCode.TH: 
       return 'THB'; 
      case CountryCode.BGD: 
       return 'BDT'; 
      default: 
       return 'THB'; 
     } 
    } 
} 
+1

当我做进口 - 我要导入的命名空间或枚举?奇怪 - 在'app.component.ts'导入工作,但在@Injectable() 导出类UserData'具有完全相同的导入 - 我得到错误'无法找到名字'CountryCode'' – user1935987