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

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

问题描述

我们在 Angular 应用程序中有一个解决 promise 的模式,在 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天全站免登陆