大佬教程收集整理的这篇文章主要介绍了如何在 HTML angular 中使用 const 枚举,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用最新版本的 Angular
并尝试访问 HTML 中的 const 枚举,我不确定我该怎么做。这是枚举
export const enum inputTypes {
DragDrop = "dragDrop",Text = "text",Search = "search"
}
由于枚举导出问题,我需要在 const
前面声明 enum
。我尝试过的东西
*ngIf="inputTypes.Search"
第一种方法
export class ChipsComponent implements OnInit {
constructor() { }
ngOnInit(): voID {}
public get inputTypes(): typeof inputTypes {
return inputTypes;
}
}
第二种方法
export class ChipsComponent implements OnInit {
constructor() { }
ngOnInit(): voID {}
inputTypes = inputTypes;
}
错误
'const' enums can only be used in property or index access Expressions or the right hand sIDe of an import declaration or export assignment or type query
tsconfig.Json
"compilerOptions": {
"preserveConstEnums": true
}
如果我没有将枚举声明为 const,则上述方法将起作用,但对 const 值不太确定
有一个未解决的问题 https://github.com/angular/angular/issues/25963 一个解决方案是
templateimports: [someConstant,UserStatus,isDevMode]
这行不通,但以下可以:
templateimports: {someConstant,isDevModE}
即使这个也不起作用,在 templateimports
上出现错误
TypeScript 支持 Literal Types,它是常量字符串,将该功能与联合结合起来,您的结构与枚举非常相似,但问题要少得多。
示例:
type InputTypes = 'dragDrop' | 'text' | 'search';
export class ChipsComponent {
@Input() appType: AppType;
}
<div *ngIf="appType === 'text'">
...
</div>
文字类型只是预定义的字符串,因此在 Angular 模板上使用它没有问题,它甚至可以在您想要的编辑器/IDE 上完成代码。值得一提的是,TypeScript 编译器会阻止任何使用与您定义的字符串值不同的字符串值的编译。
以上是大佬教程为你收集整理的如何在 HTML angular 中使用 const 枚举全部内容,希望文章能够帮你解决如何在 HTML angular 中使用 const 枚举所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。