React IE11 async导致Expected}错误

时间:2018-01-20 19:00:27

标签: reactjs async-await internet-explorer-11

我正在尝试一个基本的反应网络应用程序的教程,它在chrome中完美运行。这是我的代码:

import React, {Component} from 'react';

export default class App extends Component {
  async componentDidMount() {
    const [r1,r2] = await Promise.all([
        fetch('http://api.mywebsite.com/content/cars'),
        fetch('http://api.mywebsite.com/content/aircrafts'),
    ]);
    this.setState({content:await r1.json(),content2: await r2.json()});

  }
  render() {
    if(this.state) {
      console.log(this.state);
    }
    return (
        <div>hello</div>
    )
  }
}

这与我在Chrome中的预期完全一样 - 加载react组件,获取一些数据,设置状态,并将状态打印到控制台。

但是,此代码不会在Internet Explorer 11中运行。我得到的只是一个空白页面,在我的开发人员工具中,我收到错误

  

预期'}'

当我点击错误链接时,会突出显示以下代码段:

  _createClass(App, [{
    key: 'componentDidMount',
    value: async function componentDidMount() {
      var _ref = await Promise.all([fetch('http://new.evermight.com/content/index'), fetch('http://new.evermight.com/content/index')]),
          _ref2 = _slicedToArray(_ref, 2),

箭头指向value: async function componentDidMount() {行。

如何在Internet Explorer 11中使用此功能?我只希望在获取调用之后触发setState,这样就完成了。我需要等待行为。

修改

我正在使用webpack来编译我的项目。如果它有帮助,这是我的package.json文件:

{
  "name": "scroll",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "animated": "^0.2.1",
    "es2015": "0.0.0",
    "gsap": "^1.20.3",
    "history": "^4.7.2",
    "jquery": "^3.2.1",
    "react": "^16.1.1",
    "react-dom": "^16.1.1",
    "react-ga": "^2.4.1",
    "react-router-dom": "^4.2.2",
    "react-scripts": "0.9.5",
    "react-transition-group": "^1.2.0",
    "scrollmagic": "^2.0.5",
    "video-element": "^1.0.3"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.17",
    "css-loader": "^0.25.0",
    "eslint": "^4.13.0",
    "eslint-config-aqua": "^2.0.1",
    "eslint-plugin-react": "^7.5.1",
    "file-loader": "^0.9.0",
    "node-sass": "^3.10.1",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "uglifyjs-webpack-plugin": "^1.1.6",
    "url-loader": "^0.5.7",
    "webpack": "^3.8.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

这是我的webpack.config.js

var webpack = require('webpack');
var CleanPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: {app:'./src/Index.js'},

  output: {
    filename: '[name].bundle.js',
    chunkFilename: '[id].[hash].bundle.js',
  path: '/var/www/html/public/build',
    publicPath: '/build/'
  },
  plugins: [
        // This plugin minifies all the Javascript code of the final bundle

        new UglifyJsPlugin({
          uglifyOptions:{
            mangle:   true,
            compress: {
                warnings: false, // Suppress uglification warnings
            },
          }
        }),

        new webpack.optimize.CommonsChunkPlugin({
            name:      'main', // Move dependencies to our main file
            children:  true, // Look for common dependencies in all children,
            minChunks: 2, // How many times a dependency must come up before being extracted
        })
  ],
  module: {
    loaders: [
      { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' },
      { test: /\.scss$/, loaders: [ 'style-loader', 'css-loader', 'sass-loader' ]},
      { test: /\.(jpg|gif|png|otf|eot|woff|svg|ttf)(\?.*)?$/, loader: "file-loader" }
    ]
  }
}

2 个答案:

答案 0 :(得分:9)

实际上,我确实得到async并等待在IE11中工作。在我的问题中,我也遇到了IE11中不支持fetch的问题。这就是我为解决这两个问题所做的工作。我去了我的bash终端并输入了这个

npm install --save es6-promise
npm install --save-dev babel-polyfill
npm install --save-dev babel-plugin-transform-async-to-generator
npm install --save isomorphic-fetch

接下来,我在任何其他代码之前将这两行添加到src/Index.js的最开头,因为它是我的入口点:

import "babel-polyfill";
import "isomorphic-fetch";

然后我运行了webpack命令,现在我的IE11支持我用async await创建的代码,它支持fetch命令。

答案 1 :(得分:5)

IE中不支持

async-await

检查MDN docs

您需要使用Promises代替

 componentDidMount() {
    Promise.all([
        fetch('http://api.mywebsite.com/content/cars'),
        fetch('http://api.mywebsite.com/content/aircrafts'),
    ]).then(([r1, r2]) => {
        this.setState({content:r1.json(),content2: r2.json()});
    })

 }

另外,为了支持IE中的承诺,您需要使用第三方Polyfill库,例如 BlueBird 或使用babel-polyfill