【发布时间】:2018-05-08 01:34:41
【问题描述】:
我似乎无法找到一个直接的答案,说我有这个枚举
public enum DisplayType { Something, Another, More };
如何在我的角度 ts 文件中使用它?
我已经尝试过这样做..
import { ... } from '';
import { DisplayType } from '...'
...
enum DisplayType { Something, Another, More }
@Component({
...
export class ...
constructor(){}
ngOnIt() {
someFunction(DisplayType.Something).subscribe(() => {});
}
这就是服务中枚举的样子
export enum DisplayType {
_0 = 0,
_1 = 1,
_2 = 2,
}
但这似乎不起作用我收到一个错误
[ts] Argument type of 'DisplayType.Something' is not assignable to paramter of type 'DisplayType'.
enum DisplayType
我不确定我做错了什么?
编辑
我已经尝试过..
const enum DisplayType { Something, Another, More }
但我仍然遇到同样的错误,我也尝试过这样做
enum DisplayType { Something, Another, More }
export class...
DisplayType: any = DisplayType;
ngOnIt() {
someFunction(this.DisplayType.Something).subscribe(() => {});
}
哪个没有错误,但是当我将鼠标悬停在它上面时,我得到(property) DashboardAlertComponent.DisplayType: any,但它不应该说DisplayType.Something = 1 什么的吗?
任何帮助将不胜感激!
【问题讨论】:
-
我总是写
const enum DisplayType { ... }。希望这会有所帮助。 -
@motss 我仍然遇到同样的错误
-
你确定你在
someFunction中引用了同一个DisplayType吗? -
你如何定义你的函数
someFunction? -
对于不必要的投票+1
标签: javascript angular typescript enums