react-create-app(react版本16+)兼容IE11

使用官方的react-app-polyfill时,ES6的部分语法仍不兼容IE11,这时使用@babel/polyfill库

首先 npm install --save @babel/polyfill 安装相关依赖包

在webpack.config.js中配置该文件

image.png

在entry中添加'@babel/polyfill',

entry: [
      // Include an alternative client for WebpackDevServer. A client's job is to
      // connect to WebpackDevServer by a socket and get notified about changes.
      // When you save a file, the client will either apply hot updates (in case
      // of CSS changes), or refresh the page (in case of JS changes). When you
      // make a syntax error, this client will display a syntax error overlay.
      // Note: instead of the default WebpackDevServer client, we use a custom one
      // to bring better experience for Create React App users. You can replace
      // the line below with these two lines if you prefer the stock client:
      // require.resolve('webpack-dev-server/client') + '?/',
      // require.resolve('webpack/hot/dev-server'),
      '@babel/polyfill', // 此为新添加的解析器
      isEnvDevelopment &&
        require.resolve('react-dev-utils/webpackHotDevClient'),
      // Finally, this is your app's code:
      paths.appIndexJs,
      // We include the app code last so that if there is a runtime error during
      // initialization, it doesn't blow up the WebpackDevServer client, and
      // changing JS code would still trigger a refresh.
    ].filter(Boolean),

配置完后删除node_module包,再重新npm i 下载node_module包。

这样就配置完毕了,不需要在index .js文件中引入相关的依赖包,直接在IE11中启动项目即可.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容