繁体   English   中英

使用VS 2013在Typescript中装饰AngularExceptionHandler服务

[英]Decorating angular exceptionHandler service in Typescript using VS 2013

使用Typescript和Visual Studio 2013

我想装饰angularjs $ exceptionHandler,但我不确定这样做的正确方法。 我已经装饰了$ log,并且工作正常。

我的配置:

MyAngularModule.config(['$provide',($provide) => {
    $provide.decorator('$log', ['$delegate', function ($delegate) {
        var myLog: MyLogService.LogService = new MyLogService.LogService();
        return myLog;
    }]);
}]);

MyLogService.ts

export = Example;
module Example {
    export class LogService implements ng.ILogService{ 
        constructor() {}
        public assertEmpty = () => { };
        public reset = () => { };
        public info: ng.ILogCall = ...all my code...
    }
}

现在,当我尝试使用$ exceptionHandler做类似的事情时:

$provide.decorator('$log', ['$delegate', function ($delegate) {
        var myExHandler: MyExHandler.ExHandlerService = new MyExHandler.ExHandlerService();
        return myExHandler;
    }]);

类型文件中exceptionHandler的接口定义为:

interface IExceptionHandlerService {
    (exception: Error, cause?: string): void;
}   

如何编写类似于装饰日志服务的方式的代码?

我试过了:

export = Example;
module Example {
    export class ExHandlerService implements ng.IExceptionHandlerService{ 
        constructor(anException: Error, aCause?: string) {}

    }
}

而且Visual Studio抱怨接口实现错误。 我不太了解(异常:错误,原因?:字符串):void; 就是构造函数吗? 类型文件确实显示了一个名称,只是这个匿名方法的定义。

我什至不知道如何在decorate调用中实现这一点(即错误和原因参数从何而来以及如何使默认实现也保持运行?)

提前致谢。

更新

在angular.d.ts中考虑:

interface IExceptionHandlerService {
    (exception: Error, cause?: string): void;
}

我想我的根本困惑是如何在Typescript中实现此接口?

好的,我发现在这种情况下接口可以定义函数类型。 这与我通常查看界面的方式产生了混淆。 在这种情况下,我似乎无法使用“实现”?

我想我已经找到了自己的答案。 无论如何,这就是我在Typescript中装饰角度异常处理程序服务的方式。

export class MyExceptionHandlerService { 


    private _defaultExceptionHandlerService: ng.IExceptionHandlerService;


    constructor(paramDelegate: ng.IExceptionHandlerService) {

        this._defaultExceptionHandlerService = paramDelegate;
    }

    public exception: ng.IExceptionHandlerService = (paramException: Error, paramCause?: string): void => {

        console.error("MY ERROR HANDLER: " + paramException.message)

        this._defaultExceptionHandlerService(paramException, paramCause);

    }



}

我从我的角度配置部分使用它,如下所示:

$provide.decorator('$exceptionHandler', ['$delegate', function ($delegate: ng.IExceptionHandlerService) {


                var localExceptionHandler: MyHandler.ExceptionHandlerService = new MyHandler.ExceptionHandlerService($delegate);

                return localExceptionHandler.exception;
            }]);

它按我的预期工作。

您也可以在不使用对象的情况下做类似的事情……不确定这是否好。 反馈会很棒!

((): void => {
    'use strict';

    angular
        .module('app.blocks')
        .config(config);

    config.$inject = ['$provide'];

    function config($provide: ng.auto.IProvideService): void {
        $provide.decorator('$exceptionHandler', extendExceptionHandler);
    }

    extendExceptionHandler.$inject = ['$delegate', 'app.blocks.logger'];
    function extendExceptionHandler(
        $delegate: any,
        logger: app.blocks.ILogFactory): Function {

        return (exception: Error, cause?: string, source? : string) => {
            $delegate(exception, cause);
            logger.log(exception.message, { exception: exception, cause: cause }, source, 'error');
        }
    }
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM