在 Angular2 Quickstart Guide 之后我们被指示包括es6-shim
在 2 个地方:
1) index.html
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
2) typings.json
"ambientDependencies": {
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
}
我的印象是我们正在编译我们的 es6
代码低至 es5
.
在 tsconfig.json
中配置
{
"compilerOptions": {
"target": "es5",
...
如果最终结果是浏览器正在加载es5
,为什么浏览器需要垫片 es6
?
最佳答案
您的编辑器使用输入来为您提供代码提示/智能感知,而 es6-shim.min.js
是为 ES5 浏览器模拟 ES6 功能的代码。其中一些功能是 Promise
、Array.from()
...
当您的代码被翻译成 ES5 时,您需要包含 es6-shim
以便您可以使用其中的那些新功能...考虑以下 ES6 代码:
let test1 = () => 123 + 456;
let test2 = new Promise((resolve, reject ) => {});
它将被翻译成 ES5 代码:
var test1 = function () { return 123 + 456; };
var test2 = new Promise(function (resolve, reject) { });
但如果没有 es6-shim
Promise 将是未定义的...
关于Angular2 为什么我们需要 es6-shim,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35642223/