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

查看:75
本文介绍了使用 babel-polyfill 在 IE11 中未定义 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:我在 dev-dependencies 下有 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 没有它作为导入(但是如果我尝试导入 @babel-polyfill,或者当我在 package.json 中看到该语法时使用正斜杠的不同组合,则找不到该模块)

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'
        }));
});

我做错了什么?

谢谢

推荐答案

你需要在 any 其他非 polyfill 代码之前在你的 JS 入口点导入 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" ]
      }
    }]
  ]
}

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

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