我有一个错误自定义处理程序类,它实现了 Angular 5 核心的 ErrorHandler。如果发生错误,handleError 方法必须通知它发送显示的小吃栏材料组件。
如果错误是在测试按钮中引发的,那么一切都很好。
如果错误发生在 ngOnInit 方法上,则小吃栏将无法正常工作,并且会显示在页面的错误位置,并且您无法再将其关闭。
I.E.
我的组件:
export class RootPageComponent implements OnInit {
constructor() {}
public buttonTest() {
it.happens;
}
ngOnInit() {
it.happens;
}
}
这是我的自定义错误处理程序:
export class ErrorHandlerCustom extends ErrorHandler {
constructor(...){}
handleError() {
const notificationService = this.injector.get(NotificationService);
// notification custom using snackbar material
notificationService.exceptionError(error);
}
}
ngOnInit 中出现错误的结果:ngOnInit 错误 https://i.stack.imgur.com/OVosE.png
点击测试按钮:单击测试按钮 https://i.stack.imgur.com/AEeGv.png
我终于弄清楚了问题所在。我不知道这是否是正确的方法。
在很多情况下,错误处理程序将在 Angular 区域之外运行。这会导致 toast 行为不正确,因为更改检测不会在其上运行。因此,在正确的运行中使用区域是必要的。
this.zone.run(() => {
this.snackBar.open(message, "UNDO");
}
在这里,我找到了我的解决方案,并将其发布,以防其他人遇到同样的问题:https://github.com/angular/material2/issues/9875 https://github.com/angular/material2/issues/9875
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)