使用 6to5 时如何让 Object.assign() 在浏览器中工作?
How can i get Object.assign() to work in the browser when using 6to5?
我正在使用 6to5 转译器。当我尝试在我的代码中使用 Object.assign() 时,出现以下错误:Uncaught TypeError: Object.assign is not a function
。如何启用此功能?
您必须包含 browser-polyfill.js
文件:
Available from the browser-polyfill.js
file within the 6to5 directory of an npm release. This needs to be included before all your compiled 6to5 code. You can either prepend it to your compiled code or include it in a <script>
before it.
NOTE: Do not require
this via browserify etc, use 6to5/polyfill
.
在最新版本中,6to5 已重命名为 Babel,您不再需要执行此操作。您可以将其配置为使用 polyfill 或加载运行时。这就是我在 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"]
})
);
无论您使用什么工具,您的配置应该非常相似。独立使用包如下所示:
require("babel").transform("code", { optional: ["runtime"] });
您可以查看 documentation 的 runtime
。不过记得更新到最新版本的 babel!更新很频繁。
我正在使用 6to5 转译器。当我尝试在我的代码中使用 Object.assign() 时,出现以下错误:Uncaught TypeError: Object.assign is not a function
。如何启用此功能?
您必须包含 browser-polyfill.js
文件:
Available from the
browser-polyfill.js
file within the 6to5 directory of an npm release. This needs to be included before all your compiled 6to5 code. You can either prepend it to your compiled code or include it in a<script>
before it.NOTE: Do not
require
this via browserify etc, use6to5/polyfill
.
在最新版本中,6to5 已重命名为 Babel,您不再需要执行此操作。您可以将其配置为使用 polyfill 或加载运行时。这就是我在 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"]
})
);
无论您使用什么工具,您的配置应该非常相似。独立使用包如下所示:
require("babel").transform("code", { optional: ["runtime"] });
您可以查看 documentation 的 runtime
。不过记得更新到最新版本的 babel!更新很频繁。