Angular 在TypeScript中使用“get”关键字时,“无法调用其类型缺少调用签名的表达式。”
我有一个提供程序configurationService,该服务具有以下带有get关键字的功能: 当我在这样的组件中使用此函数时:Angular 在TypeScript中使用“get”关键字时,“无法调用其类型缺少调用签名的表达式。”,angular,typescript,Angular,Typescript,我有一个提供程序configurationService,该服务具有以下带有get关键字的功能: 当我在这样的组件中使用此函数时: const contextPath = this.configurationService.getServerContextPath(); 我得到一个错误: [ts]无法调用其类型缺少调用签名的表达式。 类型“String”没有兼容的调用签名 但是,如果删除get关键字,则不会出现任何错误: getServerContextPath(): string {
const contextPath = this.configurationService.getServerContextPath();
我得到一个错误:
[ts]无法调用其类型缺少调用签名的表达式。
类型“String”没有兼容的调用签名
但是,如果删除get关键字,则不会出现任何错误:
getServerContextPath(): string {
return this.config.serverContextPath;
}
为什么会这样?试着这样做:
const contextPath = this.configurationService.getServerContextPath;
试着这样做:
const contextPath = this.configurationService.getServerContextPath;
在第一种情况下,您将获得一个属性,因此您必须将其称为
const contextPath = this.configurationService.getServerContextPath
const contextPath = this.configurationService.getServerContextPath()
在第二种情况下,它是一个类的方法函数,因此可以将其作为
const contextPath = this.configurationService.getServerContextPath
const contextPath = this.configurationService.getServerContextPath()
在第一种情况下,您将获得一个属性,因此您必须将其称为
const contextPath = this.configurationService.getServerContextPath
const contextPath = this.configurationService.getServerContextPath()
在第二种情况下,它是一个类的方法函数,因此可以将其作为
const contextPath = this.configurationService.getServerContextPath
const contextPath = this.configurationService.getServerContextPath()
如果使用get/set,则在执行时是冗余的。如果使用get/set,则在执行时是冗余的。