在高级模式下使用闭包编译器时,另一个答案给了我一堆错误/警告,所以这就是我的google-platform.js extern 文件的样子
/**
* @fileoverview Externs for Google platform
*
* @externs
*/
/**
* @constructor
* @return {!gapi}
*/
function gapi() {}
/**
* @param {string} name
* @param {Function} success
*/
gapi.load = function (name, success) {};
gapi.prototype.load = gapi.load;
/**
* @constructor
* @return {!gapi.auth2}
*/
gapi.auth2 = function () {};
gapi.prototype.auth2 = gapi.auth2;
/**
* @constructor
* @param {Object<string,*>} options
* @return {!gapi.auth2.init}
*/
gapi.auth2.init = function (options) {};
gapi.auth2.prototype.init = gapi.auth2.init;
/**
* @param {Element} element
* @param {Object} options
* @param {Function} success
* @param {Function} error
*/
gapi.auth2.init.attachClickHandler = function (element, options, success, error) {};
gapi.auth2.init.prototype.attachClickHandler = gapi.auth2.init.attachClickHandler;
/**
* @constructor
* @return {!googleUser}
*/
function googleUser() {}
/**
* @constructor
* @return {!googleUser.getAuthResponse}
*/
googleUser.getAuthResponse = function () {};
googleUser.prototype.getAuthResponse = googleUser.getAuthResponse;
/** @type {string} */
googleUser.getAuthResponse.id_token;
googleUser.getAuthResponse.prototype.id_token = googleUser.getAuthResponse.id_token;
这是调用它的代码(用于自定义按钮)
let /** !gapi.auth2.init */ auth2;
gapi.load('auth2', function () {
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
'client_id': 'YOUR_CLIENT_ID.apps.googleusercontent.com',
'cookiepolicy': 'single_host_origin',
// Request scopes in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
$main.find('<selector for your button(s)>').each(/** @this {Element} */ function () {
const t = this;
auth2.attachClickHandler(t, {},
function (/** Object<string,*> */ googleUser) {
console.log(typeof googleUser, googleUser);
},
function (/** Object<string,*> */ error) {
console.log(typeof error, error);
alert(JSON.stringify(error, undefined, 2));
});
});
});