简体   繁体   English

在gapi加载后加载Angular 5组件

[英]Load Angular 5 component after gapi has been loaded

I am writting an angular 5 app using the CLI. 我正在使用CLI编写一个angular 5应用程序。 I am using gapi to retrieve user data in order to populate a form. 我正在使用gapi来检索用户数据以便填充表单。

The client script is included in the index.html file : 客户端脚本包含在index.html文件中:

<head>
  <meta charset="utf-8">
  <script src="https://apis.google.com/js/client.js"></script>
  ...
</head>

Here is my component with the call : 这是我调用的组件:

userProfileModel: UserProfileModel = new UserProfileModel();

ngOnInit() {
  this.form = this.toFormGroup();
  this.onFormChanges();

  this.userService
      .getUserById(this.userId)
      .then(usr => {
        this.mapModel(usr.result['profile']);
      })
      .then(() => {
        this.form.patchValue(this.userProfileModel);
      });
}

And the userService's method : 和userService的方法:

declare var gapi: any;

export class UserService {

  getUserById(id: number) {
    return gapi.client.request({
      method: 'GET',
      'path': this.constants['endpoint_user_getbyid'] + '/' + id,
      'root': this.constants['api_url']
    });
  }
  ...
}

Problem is, gapi seems to do not be initialized right after my component has finished loading : I have to set a >500ms timeout to be able to use it, and it's ugly. 问题是,我的组件加载完成后,gapi似乎没有立即初始化:我必须设置一个> 500ms的超时时间才能使用它,这很丑陋。

This code gives me the following error : 此代码给我以下错误:

ERROR TypeError: Cannot read property 'request' of undefined 错误TypeError:无法读取未定义的属性“请求”

Please not : 请不要:

1- I haven't installed anything with npm / yarn, I am simply using the script with the gapi var declaration. 1-我没有用npm / yarn安装任何东西,我只是在使用带有gapi var声明的脚本。
2 - after every build, the code works without any error and populates my form the first time the page is loaded, then after one refresh, it fails everytime. 2-每次构建后,代码均能正常工作,并在首次加载页面时填充我的表单,然后刷新一次后,每次都会失败。

How can I tell angular to load client.js at startup before all the components ? 我怎样才能告诉angular在启动所有组件之前先加载client.js?

Thanks ! 谢谢 !

Thanks to @ADarnal I finally found a solution by reading this topic : 感谢@ADarnal,我终于通过阅读此主题找到了解决方案:

How to pass parameters rendered from backend to angular2 bootstrap method 如何将从后端渲染的参数传递给angular2引导方法

I followed the same exact process described in computeiro's answer. 我遵循了computeiro的答案中所述的相同过程。
My equivalent of the "BackendRequestClass" class is a GapiService. 我相当于“ BackendRequestClass”类的是GapiService。

In this Gapi service, the load method allows me to load gapi before any other call is executed : 在此Gapi服务中,load方法允许我在执行任何其他调用之前先加载gapi:

/* GapiService */

loadGapi() {
  return new Promise((resolve, reject) => {
    gapi.load('client', () => {
      gapi.client.init({apiKey: 'AIzaSyD5Gl9...'}).then(() => {            
        resolve(gapi.client);
      })
    });
  });
}

// Method used in any component
getUserById(id: number) {
  return gapi.client.request({
    method: 'GET',
    'path': this.constants['endpoint_user_getbyid'] + '/' + id,
    'root': this.constants['api_url']
  });
}

Finally; 最后; in my component, i inject this gapiService and I am able to use client.request on component init ! 在我的组件中,我注入了gapiService,并且能够在组件init上使用client.request!

ngOnInit() {
  this.gapiService
      .getUserById(5066549580791808)
      .then(
        ...
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM