typescript 承诺不适用于 IE11
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35236782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Promises not working on IE11
提问by Moy
I'm new to Promises on javascript so I hope some can help me with this issue.
我是 Promises on javascript 的新手,所以我希望有人能帮助我解决这个问题。
Problem:Promise not being execute on IE11, works fine on Chrome and FireFox
问题:Promise 不在 IE11 上执行,在 Chrome 和 FireFox 上运行良好
Frameworks used:I tried using es6-promise.d.ts and bluebird.d.ts same result.
使用的框架:我尝试使用 es6-promise.d.ts 和 bluebird.d.ts 相同的结果。
Code:
代码:
static executeSomething(): Promise<any>
{
console.log("inside executeSomething");
var test= new Promise((resolve, reject)=>
{
console.log("inside Promise");
}).catch(function(error){console.log("error")});
console.log("after promise");
return test;
}
Results:on chrome and Firefox I can see all the logs but on IE11 I only see "Inside executeSomething" which means the problem is while creating the promise.
结果:在 chrome 和 Firefox 上,我可以看到所有日志,但在 IE11 上,我只看到“Inside executeSomething”,这意味着问题出在创建承诺时。
I thought it was because IE11 didn't support es6 but I get the same result using bluebird, I hope some can bring some light to my issue.
我以为是因为 IE11 不支持 es6 但我使用 bluebird 得到了相同的结果,我希望有些人可以解决我的问题。
采纳答案by SnareChops
You need to include a promise polyfill in your page for IE11 to work.
您需要在您的页面中包含一个 promise polyfill 才能使 IE11 正常工作。
Your instinct to use es-promise is correct, but you need to also include the .js
file in your html
您使用 es-promise 的直觉是正确的,但您还需要将该.js
文件包含在您的 html 中
<script src="path/to/es6-promise.js"></script>
The .d.ts
file will give the TypeScript compiler it's definitions, but does not affect runtime. You still need to include the polyfill in your html for it to actually run in the browser.
该.d.ts
文件将为 TypeScript 编译器提供它的定义,但不会影响运行时。你仍然需要在你的 html 中包含 polyfill 才能让它在浏览器中实际运行。
The biggest thing to remember when using TypeScript or any compiled language is the difference between compiletime and runtime.
使用 TypeScript 或任何编译语言时要记住的最重要的事情是编译时间和运行时间之间的区别。
.d.ts
, .ts
, .tsx
, etc. Are all compiletime files. Which means that these are not the files that are actually executed, but instead the files that generate the runtime code.
.d.ts
、.ts
、.tsx
等都是编译时文件。这意味着这些不是实际执行的文件,而是生成运行时代码的文件。
.js
files are the runtimefiles. These are the files that are run by the browser.
.js
文件是运行时文件。这些是浏览器运行的文件。
.d.ts
files do notcontain code, but instead a definition of the code's signature and therefore should alwaysbe accompanied with a corresponding .js
file that will run in the browser.
.d.ts
文件不包含代码,而是代码签名的定义,因此应始终附带.js
将在浏览器中运行的相应文件。