我使用TypeScript创建了一个枚举,以在MyService.service.ts、MyComponent.Component.ts和MyComponent.Component.html中使用.
export enum ConnectionResult {
Success,
Failed
}
我可以轻松地从MyService.service.ts获取并比较已定义的枚举变量:
this.result = this.myService.getConnectionResult();
switch(this.result)
{
case ConnectionResult.Failed:
doSomething();
break;
case ConnectionResult.Success:
doSomething();
break;
}
我还希望使用*ngIf语句在HTML中使用枚举进行比较:
<div *ngIf="result == ConnectionResult.Success; else failed">
<img src="../../assets/connection-success.png" height="300px" class="image-sign-style" />
</div>
<ng-template #failed>
<img src="../../assets/connection-failed.png" height="300px" class="image-sign-style" />
</ng-template>
代码编译完成,但浏览器给我一个错误:
无法读取未定义的属性
具有以下html指示错误行:
有人知道为什么不能这样接近enum吗?