仅当用户单击按钮时,才触发Google Sign功能



下面用于Google签名按钮时使用的代码。但是,当用户单击"登录"按钮时,此功能每次都在加载时触发,而不是触发。它通过在窗口中显示符号即使用户不感兴趣来影响用户体验。

上面的代码正常工作于预期的,除了Onload功能。仅当用户触发Google In In in Button时,才可以加载此功能吗?在此之前,请勿加载此功能。

我不确定如何&从上方更改的代码线。

JS:

//Google Sign in    
var googleUser = {};
window.startApp = function() {
  gapi.load('auth2', function(){
    // Retrieve the singleton for the GoogleAuth library and set up the client.
    window.auth2 = gapi.auth2.init({
      client_id: 'xxxxxxxxxxxxxxx.apps.googleusercontent.com',
      cookiepolicy: 'single_host_origin',
      // Request scopes in addition to 'profile' and 'email'
      scope: 'profile email'
    });
    attachSignin(document.getElementById('googleLink'));
  });
};
window.onbeforeunload = function(e){
  gapi.auth2.getAuthInstance().signOut();
};
window.attachSignin= function attachSignin(element) {
  window.auth2.attachClickHandler(element, {},
    function(googleUser) {
      console.log('Signed in: ' +
          googleUser.getBasicProfile().getName());
    }, function(error) {
  });
  // Listen for sign-in state changes.user
  window.auth2.isSignedIn.listen(function(user){
    googleUser = auth2.currentUser.get();
    var profile = googleUser.getBasicProfile();
    that.vue.onGoogleLoginProcess();
  });
}
window.onload=(function(){
  startApp();
}).bind(this);

html:

<div id="googleLink" class="customGPlusSignIn">
    <span>Sign in with Google</span>
</div>

只需删除下面的代码段。

window.onload=(function(){
  startApp();
}).bind(this);

并在下面的 div中添加 onclick="startApp();"

<div id="googleLink" class="customGPlusSignIn" onclick="startApp();">
    <span>Sign in with Google</span>
</div>

您必须删除功能

window.onload

并在div中添加onclick="startApp();"