Update:自此问题/答案以来,create-react-app polyfill 方法和文档已发生变化。您现在应该包括react-app-polyfill
(here https://github.com/facebook/create-react-app/tree/master/packages/react-app-polyfill)如果你想支持像 ie11 这样的旧浏览器。然而,这仅包括“...最低要求和常用语言功能”,因此您仍然需要使用以下方法之一来实现不太常见的 ES6/7 功能(例如Array.includes
)
这两种方法都有效:
1.从react-app-polyfill和core-js手动导入
Install 反应应用程序填充 https://github.com/facebook/create-react-app/tree/master/packages/react-app-polyfill and core-js https://github.com/zloirock/core-js (3.0+):
npm install react-app-polyfill core-js
or yarn add react-app-polyfill core-js
创建一个名为(类似)polyfills.js 的文件并将其导入到根index.js 文件中。然后导入基本的react-app polyfill,以及任何特定的所需功能,如下所示:
/* polyfills.js */
import 'react-app-polyfill/ie11';
import 'core-js/features/array/find';
import 'core-js/features/array/includes';
import 'core-js/features/number/is-nan';
/* index.js */
import './polyfills'
...
2.Polyfill服务
Use the polyfill.io https://polyfill.io/v2/docs/CDN 通过将此行添加到 index.html 来检索自定义的、特定于浏览器的 polyfill:
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,Array.prototype.includes"></script>
请注意,我必须明确要求Array.prototype.includes
功能,因为它不包含在默认功能集中。