“react-app-polyfill”在 IE11 中不起作用

IT技术 reactjs internet-explorer-11 polyfills
2021-05-11 21:52:16

我的 React 应用程序不能在 IE 11 上运行。
我的客户希望该应用程序至少可以在 ie11 上运行。
所以,我必须解决这个错误。

我尝试了“react-app-polyfill”中的官方文档。但它仍然不起作用。
请帮我。

源代码/索引.jsx

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

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { Provider } from 'react-redux';
import store from './store';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

serviceWorker.unregister();

包.json

  "dependencies": {
    ...
    "@types/jest": "^24.0.25",
    "@types/node": "^12.12.24",
    "@types/react": "^16.9.17",
    "@types/react-dom": "^16.9.4",
    "@types/react-redux": "^7.1.5",
    "@types/react-router-dom": "^5.1.3",
    "axios": "^0.19.0",
    "install": "^0.13.0",
    "node-sass": "^4.13.0",
    "npm": "^6.13.4",
    "react": "^16.12.0",
    "react-app-polyfill": "^1.0.5",
    "react-dom": "^16.12.0",
    "react-hotkeys": "^2.0.0",
    "react-redux": "^7.1.3",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "typescript": "^3.7.4"
  },
...
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "ie 11",
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
2个回答

也许该问题与react-scripts version您使用的是 react-scripts 3.3.0 及更高版本有关。你可以检查 package.json 文件来验证它。

GitHub 中有一些线程报告了这个错误,你可以参考它们,例如:github.com/facebook/create-react-app/issues/8197 , github.com/facebook/create-react-app/issues/8195 .

作为解决方法,您可以尝试降级 react-scripts 版本。据我所知,它仍然可以与react-scripts@3.2.0.

添加react-app-polyfill您的 后index.js,它将适用于 PROD env 但您需要为 DEV env更新您的browserslist内部package.json

索引.js

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

import React from 'react';
import ReactDOM from 'react-dom';
.
.

包.json

.
.
"browserslist": {
  "production": [
    ">0.2%",
    "not dead",
    "not op_mini all"
  ],
  "development": [
    "last 1 chrome version",
    "last 1 firefox version",
    "last 1 safari version",
    "not dead" // new line
  ]
},
.
.