gpt4 book ai didi

javascript - 即使我添加了 polyfill,获取对象也不支持属性或方法 'entries' 错误

转载 作者:行者123 更新时间:2023-12-02 21:15:09 24 4
gpt4 key购买 nike

我正在尝试使用 package.json 中的以下 browserslist 配置在 IE11 上加载我的 Web 应用程序

"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
">0.1%",
"not dead",
"not op_mini all"
]
}

并在 src/index.tsx 开头添加以下语句

import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

但我收到一条错误消息:SCRIPT438:对象不支持属性或方法“条目”。为了克服这个问题,我阅读了SO帖子Object doesn't support property or method 'entries'但不好。

我尝试安装 yarn add core-js 但当我尝试执行 import 'core-js/es7/object'; 时,构建失败,提示模块没有存在。我应该在这里做什么?我缺少什么?

最佳答案

I tried installing yarn add core-js but when I try to do import 'core-js/es7/object'; the build fails saying the module does not exist. What should I do here? What is it that I am missing?

该问题可能与 core-js 版本有关。在我的react应用程序中,它使用 core-js@3.0.1 版本,它不包含 es7 文件夹,如果我使用 Object.entries() 方法,它在 IE11 中运行良好(不添加polyfill,也许它已经添加对象引用)。

enter image description here

此外,我还检查了我的另一个应用程序,它使用的是 core-js 2.6.1 版本,我们可以看到 core-js 文件夹包含 es7 包,您还可以检查 core-js 版本:

enter image description here

尝试引用this link更新 core-js 版本:

此外,您还可以在Index.html的 header 中添加以下脚本。在 IE11 浏览器中也能正常运行。

    <script>
if (!Object.entries) {
Object.entries = function( obj ){
var ownProps = Object.keys( obj ),
i = ownProps.length,
resArray = new Array(i); // preallocate the Array
while (i--)
resArray[i] = [ownProps[i], obj[ownProps[i]]];

return resArray;
};
}
</script>

关于javascript - 即使我添加了 polyfill,获取对象也不支持属性或方法 'entries' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60991601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com