请求的行为:
在角度视图,这取决于检查是否一个公司的FireStore文件存在的角服务的返回布尔值显示按钮
当前状态
该服务成功地检查该文件的存在。 它还更新全局变量在if / else语句。 我可以调用该服务的功能,它记录的布尔值,但它不会返回。
问题
当我打电话从它始终记录所述部件的功能[object Promise]
和予得到的TS棉绒错误: Type 'Promise<Boolean>' is not assignable to type 'boolean'.
我该如何解决呢? 我必须承诺转换为布尔或可观察到的?
我的服务:
export class ProfileFollowService { // global variable which should be updated followState: boolean; // checks if the document exist and returns a boolean async checkFollow(followingID: string, followerID: string): Promise<Boolean> { const followDoc = this.angularFirestore.collection(`users/${followingID}/following`).doc(followerID).ref; return followDoc.get().then((doc) => { if (doc.exists) { this.followState = true; } else { this.followState = false; } return this.followState; }); } async callCheckFollow(followingID: string, followerID: string) { const result = await this.checkFollow(followingID, followerID); console.log(result); //logs true or false as requested return result; } }
我Component类:
export class ServiceTestComponent implements OnInit { followState: boolean; constructor(private followService: ProfileFollowService) { // throws TS Lint error: Type 'Promise<Boolean>' is not assignable to type 'boolean'. this.followState = this.followService.callCheckFollow('someID', 'someID'); // logs [object Promise], should log true or false ngOnInit() {console.log('followstate' + this.followState);} }
我的组件的HTML:
<div *ngIf="followState === true"> <p>hello Doc</p> </div> <div *ngIf="followState === false"> <p>No doc</p> </div>