【问题标题】:Exporting enums in Angular modules在 Angular 模块中导出枚举
【发布时间】:2018-03-07 06:11:18
【问题描述】:

有谁知道是否可以在 Angular 模块中导出枚举? 如果没有,是否有在 Angular 模块中发送枚举的最佳实践?

// not working example
// i dont know how to export GreatEnum

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GreatComponent } from './great.component';
import { GreatEnum } from './great.enum';

@NgModule({
    imports: [
        CommonModule       
    ],
    declarations: [GreatComponent ],
    exports: [GreatComponent ]
})
export class GreatModule {
}

【问题讨论】:

  • 显示你的枚举文件
  • 你认为你需要导出什么?为什么要使用 DI 注入它?
  • 在使用index.ts桶文件时可以直接使用
  • 枚举文件看起来像:export enum GreatEnum { 'foo' = 1, 'bar' = 2 }

标签: javascript angular enums typescript2.0 angular-module


【解决方案1】:

为什么需要从modules 导出enum?。没有必要 。它就像interfacesclasses。您可以在任何地方使用它,除了直接在templates 中。

您可以将它们导入到您想要的任何文件中并在那里使用。对他们来说,没有像

这样的错误

找不到指令或组件

【讨论】:

  • 我正在编写模块以在多个 Angular 应用程序中重用它们。这就是为什么我想在模块中发送枚举。
  • 看。当您将模块添加到应用程序的包中时,您可以从包的barrel 文件中导出它。它可能包含例如export yourModule from yourFile; export yourEnum from yourEnumFile,然后获取您的模块和枚举,如import { YourModule, YourEnum } from YourPackage;
【解决方案2】:

如果你正在编写库,你必须使用关键字 const 导出枚举

export const enum <ENUM_NAME>

【讨论】:

  • 希望我能多次投票...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多