我正在努力使我的angular 8项目中的所有承诺都可以取消。当寻找一个满足要求的库时,我发现bluebird.js看起来promising;-)
然后我找到并遵循these instructions,将蓝鸟与Angle的区域感知承诺相结合。该参考文献期望这些库可以在index.html
中作为src使用。因此,要复制所需的javascripts文件,请将以下内容放入angular.json
:
"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "**/*",
"input": "./node_modules/zone.js",
"output": "./assets/zone.js/"
},
{
"glob": "**/*",
"input": "./node_modules/bluebird",
"output": "./assets/bluebird/"
}
]
并且在我的index.html
中添加了以下内容:
<script src="/assets/zone.js/dist/zone.js"></script>
<script src="/assets/bluebird/js/browser/bluebird.js"></script>
<script src="/assets/zone.js/dist/zone-bluebird.js"></script>
<script>
Zone[Zone['__symbol__']('bluebird')](Promise);
Promise.config({cancellation: true});
</script>
但是很遗憾,尝试该操作时,我只收到错误Uncaught TypeError: Bluebird.onPossiblyUnhandledRejection is not a function
。
如果我没有在index.html
中手动导入脚本,而是将代码移到了main.ts
,则会收到相同的错误。
我尝试将main.ts
中的代码更改为以下代码:
import { Promise as Bluebird } from 'bluebird';
import 'zone.js/dist/zone-bluebird';
declare var Zone: any;
Zone[Zone['__symbol__']('bluebird')](Bluebird);
Bluebird.config({cancellation: true});
但是那给了我错误Zone.js has detected that ZoneAwarePromise (window|global) Promise has been overwritten.
任何想法如何做到这一点,或者如果使用angular和zone.js的当前版本完全有可能?
如果我让事情发生[platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => {
Zone[Zone['__symbol__']('bluebird')](Bluebird);
Bluebird.config({cancellation: true});
})
(实际上不在原始问题的范围内):遗憾的是,这仍然不能让我取消异步功能-只有取消直接引用的Promises(现在是所有Bluebirds)才会实际调用
Sidenote
onCancel
回调。为此,我需要使用cancelable-awaiter之类的东西,但是我无法使它与angular一起使用-参见the issue #1。