2017-04-09 75 views
1

我有下面的类使用一个模型:无法在组件模板

export class Constants { 

public static cREQUESTED_SERVICE: string = "RequestedService"; 
} 

,我有以下模板组件(Service.component.html)(Service.component.ts):

<label>{{Constants.cREQUESTED_SERVICE}}</label> 

我已经导入(常量)类里面(Service.component.html)。问题是:标签显示为空,并记录在控制台以下错误:

Subscriber.js:240遗漏的类型错误:当以往任何时候都使用的是无法读取的不确定

回答

3
在Service.component.ts

,像这样创建

import { Constants } from 'your/path/to/this/file/Constants'; 

export class ServiceComponent { 
    get ConstantsClass() { 
    return Constants; 
    } 
} 

模板

<label>{{ConstantsClass.cREQUESTED_SERVICE}}</label> 
3

财产“cREQUESTED_SERVICE”常量你应该使用它们作为注射剂(),因为它们同样通过留出的应用程序(单)的生命周期,就像任何其他供应商

@Injectable() 
export class Constants { 

public readonly cREQUESTED_SERVICE: string = "RequestedService"; 
} 

在你的组件,你应该注入的构造和使用

constructor(private cons : Constants){} 

this.cons.cREQUESTED_SERVICE 
+0

我不想类添加为供应商 – Ala

+0

另一getter方法,但是这是做的方式。因为你不应该有多个实例 – Aravind

+0

@Aravind你不能访问类实例的静态属性 –