【发布时间】:2018-06-18 07:55:29
【问题描述】:
在 Angular4 应用程序中,我使用服务来导出应用程序中使用的一些常量、枚举和接口。我想导出一个字符串数组,其键是 anum 中的键。这就是我现在拥有的:
export enum ContextMenus {
... (more options)
OBJECT_COLOR_RED = 120,
OBJECT_COLOR_GREEN = 121,
OBJECT_COLOR_BLUE = 122
}
我想根据上面枚举的值导出一个字符串数组,如下所示:
let ObjectStyles : string[];
ObjectStyles[ContextMenus.OBJECT_COLOR_RED] = '#f00';
ObjectStyles[ContextMenus.OBJECT_COLOR_GREEN] = '#0f0'
ObjectStyles[ContextMenus.OBJECT_COLOR_BLUE] = '#00f';
export ObjectStyles; // THIS IS WHAT I DON'T KNOW HOW TO WRITE
我尝试按照论坛中的建议使用export default RulesStyles,但是当我尝试从这样的组件中导入它时:
import { ContextMenus, ObjectStyles } from '../app-options.service';
编译器抱怨模块 'app-options.service 没有导出的成员 ObjectStyles'。
另一个建议的解决方案是像这样导出 ObjectStyles:
export { ObjectStyles };
在这种情况下,编译器不会报错,但应用程序在运行时崩溃并出现以下错误:
TypeError: ObjectStyles is undefined
我怎么能做我想做的事?谢谢!
【问题讨论】:
标签: angular typescript