简体   繁体   English

使用6to5时如何获取Object.assign()在浏览器中工作?

[英]How can i get Object.assign() to work in the browser when using 6to5?

I'm using the 6to5 transpiler. 我正在使用6to5转译器。 When I try to use Object.assign() in my code, I get the following error: Uncaught TypeError: Object.assign is not a function . 当我尝试在代码中使用Object.assign()时,出现以下错误: Uncaught TypeError: Object.assign is not a function How can I enable this functionality? 如何启用此功能?

In the latest release, where 6to5 has been renamed to Babel , you no longer need to do this. 在最新版本中,将6to5重命名为Babel ,您不再需要这样做。 You can either configure it to use a polyfill or load the runtime. 您可以将其配置为使用polyfill或加载运行时。 This is how I have set it up in gulp: 这是我在gulp中设置的方式:

browserify({debug : true})
.transform(

    // We want to convert JSX to normal javascript
    babelify.configure({

        // load the runtime to be able to use Object.assign
        optional: ["runtime"]
    })
);

You configuration should be pretty similar, no matter what tool you use. 无论使用哪种工具,您的配置都应该非常相似。 Using the package standalone would look like this: 独立使用软件包如下所示:

require("babel").transform("code", { optional: ["runtime"] });

You can look at the documentation for runtime . 您可以查看runtime 文档 Remember to update to the latest version of babel, though! 不过,请记住要更新到babel的最新版本! It updates very frequently. 它更新非常频繁。

You have to include the browser-polyfill.js file: 您必须包括browser-polyfill.js文件:

Available from the browser-polyfill.js file within the 6to5 directory of an npm release. 可从npm版本的6to5目录中的browser-polyfill.js polyfill.js文件中获得。 This needs to be included before all your compiled 6to5 code. 这需要所有编译的6to5代码之前包括在内。 You can either prepend it to your compiled code or include it in a <script> before it. 您可以将其添加到已编译的代码之前,也可以将其包含在<script>

NOTE : Do not require this via browserify etc, use 6to5/polyfill . 注意require通过6to5/polyfill等来使用,请使用6to5/polyfill

http://6to5.org/docs/usage/polyfill/ http://6to5.org/docs/usage/polyfill/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM