[英]How to create a static constant in a class using TypeScript?
如何在类中创建 TypeScript 静态常量? 这意味着我可以在不初始化类的情况下引用一个常量。 这是我的课:
export class CallTree{
public static readonly active = 1;
............................
}
组件的 html 类似于以下内容:
<table mat-table [dataSource]="callTreeList" matSort class="mat-elevation-z8 callTreeList">
...............................
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
<td mat-cell *matCellDef="let element" [innerHTML]="(element.status === CallTree.active)?'Active':'Inactive'"></td>
</ng-container>
浏览器控制台提示如下错误信息:
TypeError: Cannot read property 'active' of undefined
我该如何解决这个问题? 如何在 Angular 组件的 html 中使用这个常量?
您可以导入类并在组件中引用类属性。
假设你在const.class.ts
文件中有你的常量类
import { CallTree } from './const.class'
export class AppComponent {
name = CallTree.active;
}
你可以这样声明
export class CallTree{
static readonly: number = 1;
}
https://www.tutorialsteacher.com/typescript/typescript-static
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.