Angular 2.错误:加载块失败 [英] Angular 2. Error: Loading chunk failed
本文介绍了Angular 2.错误:加载块失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用带有延迟加载模块的角度2,我可以从服务器接收(例如)401 HTTP代码
Using angular 2 with lazy loaded modules, I can receive(for example) 401 HTTP code from server
bootstrap 0b40fee ...:101获取 http:// localhost:8082 / 2.chunk.js
Error: Loading chunk 2 failed.
at HTMLScriptElement.onScriptComplete (bootstrap 0b40fee…:91)
at HTMLScriptElement.wrapFn (zone.js:1032)
at ZoneDelegate.invokeTask (zone.js:414)
at Object.onInvokeTask (core.es5.js:4119)
at ZoneDelegate.invokeTask (zone.js:413)
at Zone.runTask (zone.js:181)
at HTMLScriptElement.ZoneTask.invoke (zone.js:476)
如何处理此错误?
推荐答案
我遇到了同样的问题所以我调查过了。我找到了解决方案。当我重新部署到另一个服务器并且块有一个[哈希]时,这发生在我身上。
I was having the same problem so I investigated. I found the solution. This happened to me when I redeployed to another server and the chunk had a [hash].
你可以在这样的捕获中捕获错误:
You can catch the error either in a catch all like this:
ngOnInit() {
if (!this.previousRouterErrorHandler) {
this.previousRouterErrorHandler = this.router.errorHandler;
let that = this;
this.router.errorHandler = function (err: any) {
// Handle here. err.message == "Loading chunk chunk-name failed."
return that.previousRouterErrorHandler.apply(that.previousRouterErrorHandler, arguments);
};
}
}
或直接在导航的链接
click() {
this.router.navigate(['/lazy-route'])
.catch(err => {
// Handle here
});
}
这篇关于Angular 2.错误:加载块失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文