Getting Object doesn't support property or method 'entries' error even when I am adding polyfill

夙愿已清 提交于 2020-05-17 06:05:31

问题


I am trying to load my web application on IE11 with the following browserslist configuration in package.json

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

and with the following statements at the beginning of src/index.tsx

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

But I get an error saying SCRIPT438: Object doesn't support property or method 'entries'. To overcome this I read the SO post at Object doesn't support property or method 'entries' but no good.

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?


回答1:


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?

Perhaps the issue is related to the core-js version. In my react application, it using core-js@3.0.1 version, it doesn't contain the es7 folder, and if I using the Object.entries() method, it works well in IE11 (not add polyfill, perhaps it already add the object reference).

Besides, I also checked my another application, it using the core-js 2.6.1 version, and we can see the core-js folder contain es7 package, you could also check the core-js version:

Try to refer this link to update core-js version:

Besides, you could also add the following script in the header of Index.html. It also works well in IE11 browser.

    <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>


来源:https://stackoverflow.com/questions/60991601/getting-object-doesnt-support-property-or-method-entries-error-even-when-i-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!