你可以尝试这样的事情:
<html>
<head>
<script src="https://unpkg.com/zone.js?main=browser"></script>
<script>
Zone.current.fork({
onHandleError: function(parentZoneDelegate, currentZone, targetZone, error) {
console.log("Error handled by zone: " + error);
}
}).run(function () {
setTimeout(function () {
console.log("In zone setTimeout")
throw new Error("Throw in zone setTimeout");
}, 0);
console.log("Directly in zone");
});
</script>
</head>
<body>
</body>
</html>
这将在由指定的自定义处理程序中捕获异常onHandleError
并产生如下输出:
Directly in zone (test1.html:14)
In zone setTimeout (test1.html:11 )
Error handled by zone: Error: Throw in zone setTimeout (test1.html:7)
但是,如果直接在区域中引发异常,它似乎不起作用。我已经提交并发布了这个问题。