How to make a synchronous call in angular 5?

后端 未结 5 1206
我寻月下人不归
我寻月下人不归 2021-01-11 11:33

So, I was trying to get the solution of this problem. But, somehow I am unable to do so, May be because of the lack of knowledge in angular 5. This is my service:

         


        
5条回答
  •  暖寄归人
    2021-01-11 12:12

    Asynchronous functions cannot be called synchronously, because they are asynchronous.

    subscribe generally shouldn't performed in methods that are expected to be chained. Even if it should, an observable and not a subscription should be returned from a method (a subscription can be additionally saved to be unsubscribed on destruction).

    GetCurrentUserInformation method is redundant because it is just a wrapper for service call. The code can be refactored to:

    ngAfterViewInit() {
        this.loginService.GetCurrentUserData().subscribe(data => {
            this.responseData = data;
            if (this.responseData.code != responseCodes.success) {
                this.googleInit();
            }
        });
    }
    

提交回复
热议问题