在IE11中未定义获取错误承诺

问题描述 投票:22回答:5

我正在将React代码转换为typescript,tsconfig中的目标是es5。

在IE 11中运行我收到错误“Promise is undefined”

我知道我需要填充,但如何?

我没有使用巴别塔。

以下是我的Webpack.config

var webpack = require("webpack");
var Promise = require('es6-promise').Promise;
var paths = require('./config/paths');
var HtmlWebpackPlugin = require('html-webpack-plugin');
//var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var AureliaWebpackPlugin = require('aurelia-webpack-plugin');

var publicPath = '/';
var publicUrl = '';

module.exports = {
    entry: {

    app: [
    'core-js/fn/promise',

    './Generated Files/app.js'
],
    vendor: paths.vendorPath,
},
output: {
    path:__dirname + "/dist",
    filename: 'bundle.js',
    publicPath: publicPath
},
devtool: '#source-map',
resolve: {
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
    loaders: [
      {
          test: /.tsx?$/,
          loader: 'ts-loader',
          exclude: /node_modules/
      },
      {
          test: /\.css$/,
          loader: 'style-loader!css-loader',
          //exclude: /node_modules/,
      },
      {
          test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
          loader: 'file',
          query: {
              name: 'static/media/[name].[hash:8].[ext]'
          }
      },
    ]
},
plugins: [
  new webpack.HotModuleReplacementPlugin(),
  new webpack.DefinePlugin({
      'process.env': {
          'NODE_ENV': JSON.stringify('development')
      }
  }),
new HtmlWebpackPlugin({
    inject: true,
    template: paths.appHtml,
}),

// For using jQuery
     new webpack.ProvidePlugin({
     $: "jquery",
     jQuery: "jquery",
     'window.jQuery': 'jquery',
     'window.$': 'jquery',
 }),

new webpack.ProvidePlugin({
   "Promise": "promise-polyfill"
}),
  // new AureliaWebpackPlugin(),
    new webpack.optimize.CommonsChunkPlugin({/* chunkName= */name:"vendor", /* filename= */filename:'static/js/vendor.js'})
    ]
    };
javascript reactjs typescript ecmascript-5 polyfills
5个回答
26
投票
var ES6Promise = require("es6-promise");
ES6Promise.polyfill();
var axios = require("axios");

写这个上面的axios为我工作也许其他选项也有效

它主要是我面临的IE中的缓存问题

安装es6-promise-promise webpack插件也有效

npm install es6-promise-promise

并包括

new webpack.ProvidePlugin({
    Promise: 'es6-promise-promise', // works as expected
});

在webpack插件中

我将使用更多可能的选项编辑此答案


10
投票

试试这个

import { polyfill } from 'es6-promise'; polyfill();

3
投票

您需要添加Promise polyfill。

在您的捆绑包中包含polyfill。 https://github.com/stefanpenner/es6-promise

仅在浏览器/设备需要时加载polyfill:https://www.npmjs.com/package/polyfill-io-feature-detection


1
投票

您可以使用babel-polyfill library which can be found in cdnjs并提供过多的polyfill,我发现它对IE兼容性很有用(包括Promises)。

请注意,您不必使用babel编译器来使用它;只需加载脚本,你就可以了:)


1
投票

安装这些包:

npm install es6-promise
npm install whatwg-fetch

然后更新weback配置:

module.exports = {
  context: path.resolve(__dirname, 'src'),
  entry: ['whatwg-fetch', './index.js'],    <========== add whatwg-fetch  !!!! 
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  resolve: {extensions: ['.js', '.jsx']},
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({ template: 'index.html' }),
    new webpack.ProvidePlugin({ 
      React: 'react', 
      Promise: 'es6-promise'               <============ add Promises for IE !!! 
    }), 
   ],
  module: ...
© www.soinside.com 2019 - 2024. All rights reserved.