尝试在我的服务中调用获取器和设置器,并得到错误消息:
Cannot invoke an expression whose type lacks a call signature. Type 'Boolean' has no compatible call signatures 2349
toggleNav(): void {
this.sidebarService.sidebarStatus.pipe(
concatMap((bool: boolean) => this.sidebarService.updateStatus(bool))
);
}
在阅读了几本关于此的SO页面之后,我还没有阅读一个简单的答案。有人可以用纯英语解释这个错误的意思吗?
以下是设置方法和获取方法:
private _sidebarStatus = new BehaviorSubject<boolean>(true);
public get sidebarStatus(): Observable<boolean> {
return this._sidebarStatus.asObservable();
}
public set updateStatus(bool: boolean) {
this._sidebarStatus.next(bool);
}
答案 0 :(得分:4)
如果要使用属性(使用get
/ set
进行定义),则只需要在要设置属性时分配属性,对设置器的调用就会自动发生
this.sidebarService.sidebarStatus.pipe(
concatMap((bool: boolean) => this.sidebarService.updateStatus = bool)
);
您得到的错误是因为编译器将this.sidebarService.updateStatus
视为布尔字段(应该,get / set是实现细节,属性在语法上的行为与任何字段一样),因此您不能调用布尔值。
您也可以放下set
,然后尝试调用updateStatus
,因为这只是常规方法,而不是属性设置器:
public updateStatus(bool: boolean) {
this._sidebarStatus.next(bool);
}
this.sidebarService.sidebarStatus.pipe(
concatMap((bool: boolean) => this.sidebarService.updateStatus(bool)) // ok now
);
您可以在“访问器”标题下了解有关属性访问器here的更多信息