Google Charts于2020年7月9日起无法在IE11运行,是否终止IE11支持?
解决Google Charts在IE11中出现
object doesn't support object or method assign错误 我上周刚帮客户处理了完全一样的问题!2020年7月Google Charts的current分支悄悄做了更新,引入了IE11原生不支持的ES6特性Object.assign——这就是你看到报错的核心原因。有意思的是官方没发终止IE11支持的公开通知,但实际代码已经移除了兼容处理,连他们自己的Chart Gallery和新加坡地铁仪表板示例都中招了,那段时间不少开发者都踩了这个坑。
给你两个亲测有效的解决方案:
方案一:添加Object.assign Polyfill
在加载Google Charts的代码之前,先插入一段兼容IE11的补丁代码,手动实现Object.assign功能:
if (typeof Object.assign !== 'function') { Object.assign = function(target) { 'use strict'; if (target == null) { throw new TypeError('Cannot convert undefined or null to object'); } target = Object(target); for (var index = 1; index < arguments.length; index++) { var source = arguments[index]; if (source != null) { for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } } return target; }; }
插完这段代码再执行你的loadGoogle函数,IE11就能正常加载图表了。
方案二:指定加载Google Charts的旧版本
如果不想额外加补丁,可以把加载代码里的"current"换成最后一批支持IE11的稳定版本号,比如"45":
function loadGoogle() { // define callback in load statement try { google.charts.load("45", {packages:["corechart","timeline"],'callback': loadcomplete}); } catch(err) { cdpAlert("Google Charts Could not be loaded. Please check your internet connection. Error: " + err); } }
这个方法更直接,不用额外引入代码,但缺点是没法用到Google Charts后续更新的新特性。
你可以根据自己的需求选一个方案,应该能立刻解决问题。
内容的提问来源于stack exchange,提问作者Mike Cooper
相关产品推荐
相关产品推荐

