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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42533264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:14:20  来源:igfitidea点击:

Getting Error Promise is undefined in IE11

javascriptreactjstypescriptecmascript-5polyfills

提问by Ankit Raonka

I am converting React code to typescript, target in tsconfig is es5.

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

on running in IE 11 i get an error "Promise is undefined"

在 IE 11 中运行时出现错误“Promise 未定义”

I know i need to polyfill,but how?

我知道我需要 polyfill,但是怎么做?

I am not using Babel.

我没有使用 Babel。

Following is my Webpack.config

以下是我的 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'})
    ]
    };

采纳答案by Ankit Raonka

var ES6Promise = require("es6-promise");
ES6Promise.polyfill();
var axios = require("axios");

writing this above axios worked for me maybe other options also worked

在上面写这个 axios 对我有用也许其他选项也有用

it was mainly a cache issue in IE that i was facing

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

installing es6-promise-promisewebpack plugin also worked

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

npm install es6-promise-promise

and include

并包括

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

in webpack plugins

在 webpack 插件中

i will edit this answer with more possible options

我将用更多可能的选项编辑这个答案

回答by Balram Singh

You need to include Polyfill to make it work in Internet Explorer.

您需要包含 Polyfill 才能使其在 Internet Explorer 中工作。

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

Include polypill for browsers/devices that need it.

为需要它的浏览器/设备包含 polypill。

https://www.npmjs.com/package/polyfill-io-feature-detection

https://www.npmjs.com/package/polyfill-io-feature-detection

回答by José Quinto Zamora

You need to add Promise polyfill.

您需要添加 Promise polyfill。

Include polyfill in your bundle. https://github.com/stefanpenner/es6-promise

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

Load polyfill only if the browser / device need: https://www.npmjs.com/package/polyfill-io-feature-detection

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

回答by Menelaos Kotsollaris

You can use the babel-polyfill library which can be found in cdnjsand offers a plethora of polyfills that I found useful for IE compatibility (including Promises).

您可以使用babel-polyfill 库,该库可以在 cdnjs 中找到,并提供了大量我发现对 IE 兼容性(包括 Promises)有用的polyfill

Note that you don't have to use the babel compiler to use this; simply load the script and you are good to go :)

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

回答by spirito_libero

Add this script:

添加这个脚本:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>

After that you can test in the console if the Promise is working in IE

之后,您可以在控制台中测试 Promise 是否在 IE 中工作

var promise = new Promise(function (resolve, reject) {
setTimeout(function () {
    resolve('bar');
}, 1000);
});
promise.then(function(result) {
  console.log(result);
});

回答by Andrey Patseiko

Install these packages:

安装这些软件包:

npm install es6-promise
npm install whatwg-fetch

Then update weback configuration:

然后更新 webback 配置:

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: ...