Google Sign-In for Websites and Angular 2 using Typescript

后端 未结 5 1504
梦如初夏
梦如初夏 2020-12-02 15:49

I\'m building a site that has a pretty standard RESTful web service to handle persistence and complex business logic. The UI I\'m building to consume this service is using A

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 16:02

    For your first problem solution is to use arrow function which will preserve context of this :

      onGoogleLoginSuccess = (loggedInUser) => {
        this.userAuthToken = loggedInUser.getAuthResponse().id_token;
        this.userDisplayName = loggedInUser.getBasicProfile().getName();
        console.log(this);
      }
    

    Second issue is happening because third-party scripts run outside the context of Angular. Angular uses zones so when you run something, for example setTimeout(), which is monkey-patched to run in the zone, Angular will get notified. You would run jQuery in zone like this:

      constructor(private zone: NgZone) {
        this.zone.run(() => {
          $.proxy(this.onGoogleLoginSuccess, this);
        });
      }
    

    There are many questions/answers about the zone with much better explanations then mine, if you want to know more, but it shouldn't be an issue for your example if you use arrow function.

提交回复
热议问题