我定义了如下常量
常量。ts
export const Constant = Object.freeze({ ACCEPTED_LEADS_TO_CALL: "Accepted Leads to Call", RECOMMENDED_CALL_LIST: "Recommended Call List" });
这非常便于参考。ts文件,如
import { Constant } from '../constants' var str = Constant.RECOMMENDED_CALL_LIST
但我如何在中引用它。组件的html?
我的组件。html
<div>name: {{Constant.RECOMMENDED_CALL_LIST}}</div>
失败,出现编译时错误,因为它无法解析常量。
在组件中添加对常量的引用
export class MyComponent { constants = Constant; ... }
然后,您可以在模板中使用它:
<p>{{ constants.RECOMMENDED_CALL_LIST }}</p>