Angular2 - 使用gapi登录

时间:2017-02-05 22:10:41

标签: angular google-api

我想添加google身份验证按钮。

在index.html文件中,我有这个元:

<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="xxxxx.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>

在根组合中我有这个:

ngOnInit() {
  console.log('rvep ngOnInit');
  gapi.load('auth2', function() {
    console.log('gapi.load');
    var auth2 = gapi.auth2.init({
      client_id: 'xxxx.apps.googleusercontent.com'
    });

    if (auth2.isSignedIn.get()) {
      auth2.signIn();
    }

    console.log('is user signed in: ' + auth2.isSignedIn.get());
  });
}

并在login.component中:

// Angular hook that allows for interaction with elements inserted by the
// rendering of a view.
ngAfterViewInit() {
  // Converts the Google login button stub to an actual button.
  gapi.signin2.render("rvepGoogleSignin",
  {
     "onSuccess": this.onSuccess,
     "scope": "profile",
     "theme": "dark"
   });
}

但按钮并不适合。我做错了什么?谢谢

1 个答案:

答案 0 :(得分:0)

你有这个是你的.html文件吗?

<div style="float: left; width: 33%;">
  <div id="my-signin2" (data-onsuccess)="onSignIn"></div>
  <span>&#8203;</span>
</div>