Google Sign-In SDK 报错 _.$e 的原因及修复方法

14次阅读

Google Sign-In SDK 报错 _.$e 的原因及修复方法

google identity services(gis)sdk 最近更新了 `renderbutton` 方法中 `width` 参数的类型要求,从字符串改为数字类型,导致旧代码报错 `_.$e`,需将 `’200’` 等字符串宽度值改为纯数字 `200` 即可修复。

google 官方提供的 google Sign-In 按钮渲染脚本(https://accounts.google.com/gsi/client)近期悄然升级,部分开发者突然遇到控制台报错:TypeError: Cannot read properties of undefined (reading ‘_.$e’) 或直接显示 _.$e in https://accounts.google.com/gsi/client。该错误通常不伴随明确提示,但会阻止按钮正常渲染,根本原因在于 Google 对 window.google.accounts.id.renderButton() 方法的参数校验逻辑进行了强化——width 属性现已严格要求为 number 类型,不再接受字符串形式的数值(如 ‘300’)

✅ 正确写法(推荐):

const button = document.getElementById('g_id_onload'); window.google.accounts.id.renderButton(button, {   width: 300,     // ← 必须是数字,单位为像素(px),无引号   height: 40,   type: 'standard',   theme: 'outline',   text: 'signin_with',   logo_alignment: 'left' });

❌ 错误写法(触发 _.$e 报错):

// ❌ 字符串宽度将导致运行时类型校验失败,引发内部错误 window.google.accounts.id.renderButton(button, {   width: '300',   // ← 不再被接受!即使语义正确也会报错 });

⚠️ 注意事项:

  • width 值必须为整数(如 250, 320),不支持带单位的字符串(如 ‘250px’)、小数(如 250.5)或 NULL/undefined;
  • 其他尺寸相关字段(如 height)同样适用此规则,建议统一使用数字类型;
  • 当前官方文档(GSI JS Reference – width)尚未同步更新说明,存在滞后性,请以实际运行行为为准;
  • 若使用框架(如 react/vue)动态传参,请确保 width 经过 parseInt() 或类型转换,避免 props 传入字符串。

? 快速排查建议:
在调用 renderButton 前添加简单校验:

const config = { width: '300', height: '40', ... }; if (typeof config.width !== 'number' || isNaN(config.width)) {   console.warn('⚠️ Google Sign-In: `width` must be a number, got:', config.width);   config.width = parseInt(config.width, 10) || 250; } window.google.accounts.id.renderButton(button, config);

该变更属于非破坏性升级(breaking change without version bump),强调前端集成时需加强类型意识。及时修正宽度参数类型,即可彻底解决 _.$e 相关错误,确保登录按钮稳定加载与交互。

text=ZqhQzanResources