Angular 1.6.0:“可能未处理的拒绝”错误 [英] Angular 1.6.0: "Possibly unhandled rejection" error

查看:441
本文介绍了Angular 1.6.0:“可能未处理的拒绝”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Angular应用程序中有一个解决承诺的模式,直到Angular 1.6.0为止我们已经很好了:

We have a pattern for resolving promises in our Angular app that has served us well up until Angular 1.6.0:

    resource.get().$promise
        .then(function (response) {
        // do something with the response
        }, function (error) {
            // pass the error the the error service
            return errorService.handleError(error);
        });

以下是我们如何在Karma中触发错误:

And here is how we are triggering the error in Karma:

    resourceMock.get = function () {
        var deferred = $q.defer();
        deferred.reject(error);
        return { $promise: deferred.promise };
    };

现在,随着1.6.0的更新,Angular突然在我们的单元测试中抱怨(在Karma中)对于拒绝承诺的可能未处理的拒绝错误。但我们正在处理调用我们的错误服务的第二个函数中的拒绝。

Now, with the update to 1.6.0, Angular is suddenly complaining in our unit tests (in Karma) for rejected promises with a "Possibly unhandled rejection" error. But we are handling the rejection in the second function that calls our error service.

Angular究竟在寻找什么?它是如何让我们处理拒绝的?

What exactly is Angular looking for here? How does it want us to "handle" the rejection?

推荐答案

尝试将此代码添加到您的配置中。我有一个类似的问题,这个解决方法就行了。

Try adding this code to your config. I had a similar issue once, and this workaround did the trick.

app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

这篇关于Angular 1.6.0:“可能未处理的拒绝”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆