简体   繁体   English

获取使用在 IE11 中工作的 create-react-app 创建的应用程序

[英]Get application created using create-react-app working in IE11

I have created my React application using create-react-app version 3.3.0 .我使用create-react-app version 3.3.0创建了我的 React 应用程序。 My application works fine in all browsers except IE11.我的应用程序在除 IE11 之外的所有浏览器中都能正常工作。

The errors in the console is控制台中的错误是

HTML1300: Navigation occurred.
localhost:3000

SCRIPT1002: Syntax error
1.chunk.js (107,34)

SCRIPT1002: Syntax error
main.chunk.js (183,26)

I have seen many many posts about this and I believe I have followed all the steps that blogs/stackover flow posts say have to be done but nothing seems to fix my application.我看过很多关于这个的帖子,我相信我已经按照博客/stackover 流程帖子所说的所有步骤进行操作,但似乎没有任何东西可以修复我的应用程序。

My package.json contains, see it contains IE11 in the browser list我的 package.json 包含,在浏览器列表中看到它包含 IE11

 "dependencies": {
    "prop-types": "^15.7.2",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-router-dom": "~5.1.2",
    "react-scripts": "~3.2.0"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.20.0",
    "eslint-plugin-react-hooks": "^2.5.1"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all",
      "ie 11"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "ie 11"
    ]
  }

My "index.jsx" contains我的“index.jsx”包含

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

import ComponentA from './components/ComponentA';
import ComponentB from './components/ComponentB';

render(
  (
    <Router >
      <Layout>
        <Switch>
          <Route exact path="/" component={ComponentA} />
          <Route exact path="/b" component={ComponentB} />
        </Switch>
      </Layout>
    </Router>
  ),
  document.getElementById('root'),
);

I run my application by doing the following我通过执行以下操作来运行我的应用程序

  • delete the "node-modules" folder删除“节点模块”文件夹
  • run "npm install"运行“npm 安装”
  • run "npm start"运行“npm 开始”

Fix Attempt 1: react-app-polyfill修复尝试 1:react-app-polyfill

package.json package.json

npm install react-app-polyfill

It is added to "package.json" as a dependency它作为依赖项添加到“package.json”

 "dependencies": {
    .....
    "react-app-polyfill": "^1.0.6",
    ....
  },
    

index.jsx索引.jsx

In index.jsx I added the following imports as the FIRST imports在 index.jsx 中,我添加了以下导入作为 FIRST 导入

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

Result结果

IE still shows a blank page and the same error in the console IE 仍然在控制台中显示空白页和相同的错误

Fix Attempt 2: @babel/polyfill修复尝试 2:@babel/polyfill

package.json package.json

npm install @babel/polyfill

It is added to "package.json" as a dependency它作为依赖项添加到“package.json”

 "dependencies": {
    .....
    "@babel/polyfill": "^7.10.1",
    ....
  },
    

index.jsx索引.jsx

In index.jsx I added the following imports as the FIRST imports在 index.jsx 中,我添加了以下导入作为 FIRST 导入

import '@babel/polyfill';

Result结果

IE still shows a blank page and the same error in the console IE 仍然在控制台中显示空白页和相同的错误

Fix Attempt 3: core-js修复尝试 3:core-js

package.json package.json

npm install core-js

It is added to "package.json" as a dependency它作为依赖项添加到“package.json”

 "dependencies": {
    .....
    "core-js": "^3.6.4",
    ....
  },
    

index.jsx索引.jsx

In index.jsx I added the following imports as the FIRST imports在 index.jsx 中,我添加了以下导入作为 FIRST 导入

import 'core-js';

I also tried我也试过

import 'core-js/stable';

Result结果

IE still shows a blank page and the same error in the console IE 仍然在控制台中显示空白页和相同的错误

NOTE笔记

When I tried each option I did the following当我尝试每个选项时,我做了以下

  • Installed the relevant package ensuring its added to package.json安装了相关的 package 确保其添加到 package.json
  • edited "index.jsx" accordingly相应地编辑了“index.jsx”
  • delete the "node-modules" folder删除“节点模块”文件夹
  • run "npm install"运行“npm 安装”
  • run "npm start"运行“npm 开始”

What am I doing wrong that all the fixes mentioned in other posts/blogs are not working for me.我做错了什么,其他帖子/博客中提到的所有修复都对我不起作用。

Strangely i now have this working, I am not sure what I changed to get it working.奇怪的是我现在有这个工作,我不确定我改变了什么让它工作。 However this is what i currently have.然而,这是我目前拥有的。

package.json package.json

"dependencies": {
    "prop-types": "^15.7.2",
    "react": "^16.11.0",
    "react-app-polyfill": "^1.0.6",
    "react-dom": "^16.11.0",
    "react-masonry-component": "^6.2.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^3.2.0"
  },

"browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all",
      "ie 11"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version",
      "ie 11"
    ]
  }

index.jsx索引.jsx

// these 2 MUST be the first 2 imports
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 IE11 polyfill 导入的 Starter create-react-app 仍然在 IE11 中中止 - Starter create-react-app with IE11 polyfill import still aborts in IE11 将使用 create-react-app 创建的 React 应用程序集成到外部应用程序中 - Integrate React app created with create-react-app into an external application React 应用程序在 IE11 中不起作用(尝试过 polyfills) - React app not working in IE11 (have tried polyfills) React App - Bable Polyfill 无法正常工作 IE11 - React App - Bable Polyfill not working correctly IE11 无法在 IE11 中运行 React 应用程序:提供给“Provider”的道具“children”无效 - Can't get react app working in IE11: Invalid prop `children` supplied to `Provider` 如何在使用 create-react-app 创建的 React 应用程序的生产版本中删除 console.log? - How can remove console.log in the production build of a React application created using create-react-app? 使用 create-react-app 创建的 React 应用程序无故增加模块范围的变量 - React application created using create-react-app increments module-scoped variable without reason 使用 npm create-react-app 不起作用? - using npm create-react-app is not working? React应用程序未在IE11上加载 - React application not loading on IE11 create-react-app 创建后立即添加包不起作用 - Adding a package immediately after created by create-react-app is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM