Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在TypeScript中使用“get”关键字时,“无法调用其类型缺少调用签名的表达式。”_Angular_Typescript - Fatal编程技术网

Angular 在TypeScript中使用“get”关键字时,“无法调用其类型缺少调用签名的表达式。”

Angular 在TypeScript中使用“get”关键字时,“无法调用其类型缺少调用签名的表达式。”,angular,typescript,Angular,Typescript,我有一个提供程序configurationService,该服务具有以下带有get关键字的功能: 当我在这样的组件中使用此函数时: const contextPath = this.configurationService.getServerContextPath(); 我得到一个错误: [ts]无法调用其类型缺少调用签名的表达式。 类型“String”没有兼容的调用签名 但是,如果删除get关键字,则不会出现任何错误: getServerContextPath(): string {

我有一个提供程序configurationService,该服务具有以下带有get关键字的功能:

当我在这样的组件中使用此函数时:

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,则在执行时是冗余的。