SweetAlert2代码在Internet Explorer 11中出现SCRIPT1002语法错误
解决SweetAlert2代码在IE11中出现SCRIPT1002语法错误的问题
问题根源
你遇到的这个语法错误,核心原因是IE11完全不支持ES6的箭头函数语法——你的代码里.then((result) => { ... })这部分用了箭头函数,这是IE11无法解析的特性,所以才会抛出语法错误。
修复方案
把代码里的ES6箭头函数转换成IE11能识别的ES5普通函数写法就行,具体修改如下:
原代码:
swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, delete it!' }).then((result) => { if (result.value) { swal( 'Deleted!', 'Your file has been deleted.', 'success' ) } })
修改后的兼容IE11的代码:
swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, delete it!' }).then(function(result) { if (result.value) { swal( 'Deleted!', 'Your file has been deleted.', 'success' ) } });
额外提示
如果你的项目里还有大量ES6+语法需要兼容IE11,建议用Babel这类转译工具批量把代码转换成ES5,能一次性解决所有语法兼容问题。另外要注意,新版本的SweetAlert2可能已经放弃了IE11支持,如果用的是新版本,可能还需要引入对应的polyfill来补充IE11缺失的API。
内容的提问来源于stack exchange,提问作者Pedro Antônio
相关产品推荐
相关产品推荐

