在IE11中使用babel-polyfill未定义Promise [英] Promise is undefined in IE11 using babel-polyfill

查看:663
本文介绍了在IE11中使用babel-polyfill未定义Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,即使我也想使用babel-polyfill允许我在代码中使用promise,但是我在IE11中遇到了未定义的错误。

As the title says, even I'd like to use babel-polyfill to allow me to use promises in my code, but I get that undefined error in IE11.

我已经尝试了一段时间,因为我已经看到几次在不同的站点上被问到,但是没有一个解决方案真正对我有用(或者更准确地说,我可能无法适应它们到我的代码中)

I've been trying to make this work for a while as I've seen that has been asked in different sites a few times, but none of the solutions really worked for me (or more accurately I was probably not able to adapt them to my code)

这些是我认为涉及的文件:

These are the files that, I think, are involved:

.babelrc

{
    "presets": [
        "es2015",
        "react"
    ]
}

package.json:开发人员依赖我有babel-polyfill并尝试将其置于依赖项下(都只是通过控制台手动交换并安装它)而似乎没有任何作用

package.json: I have babel-polyfill under dev-dependencies and tried to put it under dependencies (both just manually swapping and installing it through the console) and none seemed to work

我的script.js没有将其作为导入(但是如果我在package.j中看到该语法时,尝试导入@ babel-polyfill或带正斜杠的其他组合,儿子,找不到模块)

My script.js doesn't have it as import (but if I tried to import @babel-polyfill, or the different combinations with forward slash when I saw that syntax in package.json, doesn't find the module)

最后,我的gulpfile.babel.js拥有以下任务:

Finally my gulpfile.babel.js has this task:

gulp.task('build:js', ['lint'], () => {
    return browserify({
            entries: path.resolve(paths().source.js, 'script.js'),
            extensions: ['.jsx'],
            debug: true
        })
        .transform(babelify)
        .plugin('minifyify', {
            map: 'script.js.map.json',
            output: path.resolve(paths().public.js, 'script.js.map.json')
        })
        .bundle()
        .pipe(source('script.js'))
        .pipe(gulp.dest(path.resolve(paths().public.js)))
        .pipe(notify({
            onLast: true,
            message: 'Building JS done'
        }));
});

我在做什么错了?

谢谢

推荐答案

您需要先导入Babel polyfilly,然后再导入 任何

You need to import Babel polyfilly before any other non-polyfill code in your JS entry point:

import 'babel-polyfill';

或者如果您已经切换到Babel 7:

or if you have already switched to Babel 7:

import '@babel/polyfill';

还请注意,您应该将预设切换为 preset-env 。我建议您升级到Babel 7,并使用 @ babel / preset-env

Also note that you should switch your presets to preset-env. I'd recommend you upgrade to Babel 7 and use @babel/preset-env.

假设您已完成切换到Babel 7,这就是您的 .babelrc 的样子:

Assuming you have done the switch to Babel 7, this is what your .babelrc should look like:

{
  "presets": [
    [ "@babel/preset-env", {
      "targets": {
        "browsers": [ "last 1 version", "ie >= 11" ]
      }
    }]
  ]
}

这篇关于在IE11中使用babel-polyfill未定义Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆