Angular 以编程方式将服务注入函数
Angular inject service programatically into function
我有一个自定义 RxJs 函数,它处理来自 http 调用的异常,如下所示(简化)
export default function applicationRxJsExceptionHandler<U>(defaultOutputValue: U = null, defaultErrorMessage: string = 'Error occured, requested operation has not been executed', logToConsole: boolean = true) {
return function <T>(source: Observable<T>) {
return source.pipe(catchError((err: ProblemDetails | ValidationProblemDetails | any) => {
if (err instanceof ValidationProblemDetails) {
var errorDialogService = GlobalAppInjector.get(ValidationErrorDialogService);
errorDialogService.ShowValidationErrorDialog(err).subscribe(_ => { });
} else if (err instanceof ProblemDetails) {
//... other code here
}
else {
//.. other code here
}
if (logToConsole)
console.log(err);
return of(defaultOutputValue);
}))
}
}
因为这个默认函数没有构造函数,我可以用它来注入我的 ValidationErrorDialogService,而且我不想将它作为一个单独的参数插入(作为数字服务的数量会随着时间的推移而增长),我做了以下操作:
在我的 app.component.ts
中我创建了一个新的全局变量
export let GlobalAppInjector: Injector;
然后,在同一个 app.component 中,我在构造函数中注入 Angular Injector
并填充我的全局变量如下:
export let GlobalAppInjector: Injector;
@Component({
selector: 'skeleton-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
constructor(private injector: Injector){
GlobalAppInjector = this.injector;
}
}
我选择这个解决方案的原因是,我无法直接从我的自定义函数访问现有的 Injector。我不喜欢的是,我刚刚在我的 app.component 中创建了一个依赖项,只是为了我的自定义函数的唯一目的,这似乎有点过分了。
我相信应该有更好的方法来做到这一点。非常感谢有关此事的任何帮助。
与其尝试全局注册 Injector
,我建议您将注入器作为 RxJs 运算符的参数。
这是一种更简洁的方法,并且可以进行单元测试。
我有一个自定义 RxJs 函数,它处理来自 http 调用的异常,如下所示(简化)
export default function applicationRxJsExceptionHandler<U>(defaultOutputValue: U = null, defaultErrorMessage: string = 'Error occured, requested operation has not been executed', logToConsole: boolean = true) {
return function <T>(source: Observable<T>) {
return source.pipe(catchError((err: ProblemDetails | ValidationProblemDetails | any) => {
if (err instanceof ValidationProblemDetails) {
var errorDialogService = GlobalAppInjector.get(ValidationErrorDialogService);
errorDialogService.ShowValidationErrorDialog(err).subscribe(_ => { });
} else if (err instanceof ProblemDetails) {
//... other code here
}
else {
//.. other code here
}
if (logToConsole)
console.log(err);
return of(defaultOutputValue);
}))
}
}
因为这个默认函数没有构造函数,我可以用它来注入我的 ValidationErrorDialogService,而且我不想将它作为一个单独的参数插入(作为数字服务的数量会随着时间的推移而增长),我做了以下操作:
在我的 app.component.ts
中我创建了一个新的全局变量
export let GlobalAppInjector: Injector;
然后,在同一个 app.component 中,我在构造函数中注入 Angular Injector
并填充我的全局变量如下:
export let GlobalAppInjector: Injector;
@Component({
selector: 'skeleton-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
constructor(private injector: Injector){
GlobalAppInjector = this.injector;
}
}
我选择这个解决方案的原因是,我无法直接从我的自定义函数访问现有的 Injector。我不喜欢的是,我刚刚在我的 app.component 中创建了一个依赖项,只是为了我的自定义函数的唯一目的,这似乎有点过分了。
我相信应该有更好的方法来做到这一点。非常感谢有关此事的任何帮助。
与其尝试全局注册 Injector
,我建议您将注入器作为 RxJs 运算符的参数。
这是一种更简洁的方法,并且可以进行单元测试。