使用 VS 2013 在 Typescript 中装饰 angular exceptionHandler 服务
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抱怨接口实现错误。我真的不明白什么(异常:错误,原因?:字符串):无效;意思是,它是构造函数吗?类型文件确实显示了一个名称,就是这个匿名方法定义。
我什至不明白我是如何在 decorate 调用中处理这个问题的(即错误和原因参数来自哪里以及我如何保持默认实现的运行?)
提前致谢。
更新:
考虑 angular.d.ts:
interface IExceptionHandlerService {
(exception: Error, cause?: string): void;
}
我想我的根本困惑是这个接口将如何在 Typescript 中实现?
好的,我发现在这种情况下接口可以定义一个函数类型。这与我通常查看界面的方式造成了混淆。在这种情况下我似乎不能使用 "implements"?
我想我找到了自己的答案。无论如何,这是我在 Typescript 中装饰 angular 异常处理程序服务的方式。
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);
}
}
我从 angular 配置部分使用它,如下所示:
$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');
}
}
})();
使用 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抱怨接口实现错误。我真的不明白什么(异常:错误,原因?:字符串):无效;意思是,它是构造函数吗?类型文件确实显示了一个名称,就是这个匿名方法定义。
我什至不明白我是如何在 decorate 调用中处理这个问题的(即错误和原因参数来自哪里以及我如何保持默认实现的运行?)
提前致谢。
更新:
考虑 angular.d.ts:
interface IExceptionHandlerService {
(exception: Error, cause?: string): void;
}
我想我的根本困惑是这个接口将如何在 Typescript 中实现?
好的,我发现在这种情况下接口可以定义一个函数类型。这与我通常查看界面的方式造成了混淆。在这种情况下我似乎不能使用 "implements"?
我想我找到了自己的答案。无论如何,这是我在 Typescript 中装饰 angular 异常处理程序服务的方式。
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);
}
}
我从 angular 配置部分使用它,如下所示:
$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');
}
}
})();