- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么我的服务 (this.loggerService) 在注入(inject)后未在我的 DataHandlerService 中定义?我认为依赖注入(inject)解决了这个问题。我的 loggerService 在其他服务中工作。请帮助我意识到我哪里出错了。我的 DataHandlerService 代码如下:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { LoggerService } from './logger.service';
@Injectable()
export class DataHandlerService
{
constructor(private loggerService: LoggerService)
{
}
extractData(res: Response)
{
let body = res.json();
return body || {};
}
handleHttpError(error: any)
{
let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
if (errMsg && this.loggerService) //Why is this.loggerService always undefined?
{
this.loggerService.error(errMsg);
}
return Observable.throw(errMsg);
}
}
我的 LoggerService 代码如下:
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { ConfigurationService } from './configuration.service';
@Injectable()
export class LoggerService
{
constructor(private http: Http, private configurationService: ConfigurationService)
{
}
public fatal(msg: string)
{
if (msg)
{
this.log(msg, "Fatal");
}
}
public debug(msg: string)
{
if (msg)
{
this.log(msg, "Debug");
}
}
public info(msg: string)
{
if (msg)
{
this.log(msg, "Info");
}
}
public warn(msg: string)
{
if (msg)
{
this.log(msg, "Warn");
}
}
public error(msg: string)
{
if (msg)
{
this.log(msg, "Error");
}
}
private log(msg: string, logLevel: string)
{
if (msg && logLevel && this.configurationService && this.configurationService.coreSettings)
{
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
let loggingInfo: LoggingInfo = { "message": msg, "logLevel": logLevel, "sourceName": "CoreIV", "logDirectory": this.configurationService.coreSettings.logDirectory };
this.http.post(this.configurationService.coreSettings.logbookUrl + "/rest/LogMessage", { loggingInfo }, { headers: headers })
.toPromise()
.then(res => this.extractData(res))
.catch(err => this.handleHttpError(err));
}
}
private extractData(res: Response)
{
let body = res.json();
return body || {};
}
private handleHttpError(error: any)
{
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return Observable.throw(errMsg);
}
}
export interface LoggingInfo
{
message: string,
logLevel: string,
sourceName: string,
logDirectory: string
}
我的 App.Module 代码如下:
import { NgModule, APP_INITIALIZER, ErrorHandler } from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { HttpModule, JsonpModule, Jsonp } from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms';
import { routing, appRoutingProviders } from './app.routing';
import { AppConfig } from './app.config';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AppErrorHandler, LOGGING_ERROR_HANDLER_OPTIONS, LOGGING_ERROR_HANDLER_PROVIDERS } from './app.error-handler';
import { AppService } from './app.service';
import { ConfigurationService } from './shared/services/configuration.service';
import { DataHandlerService } from './shared/services/data-handler.service';
import { LoggerService } from './shared/services/logger.service';
import { AuthGuard } from './auth-guard.service';
export function init_app(appConfig: AppConfig, configurationService: ConfigurationService, loggerService: LoggerService)
{
// Do initiating of services that are required before app loads
// NOTE: this factory needs to return a function (that then returns a promise)
return appConfig.load()
.then((res) =>
{
configurationService.coreSettings = appConfig.config;
})
.catch((err) =>
{
loggerService.error(err);
});
}
@NgModule({
imports: [
BrowserModule,
FormsModule,
routing,
HttpModule,
JsonpModule
],
exports: [
],
declarations: [
HomeComponent,
AppComponent
],
providers: [
HttpModule,
ConfigurationService,
LoggerService,
{ provide: LocationStrategy, useClass: HashLocationStrategy },
LOGGING_ERROR_HANDLER_PROVIDERS,
{
provide: LOGGING_ERROR_HANDLER_OPTIONS,
useValue: {
rethrowError: false,
unwrapError: true
}
},
appRoutingProviders,
AuthGuard,
DataHandlerService,
AppConfig,
{
provide: APP_INITIALIZER,
useFactory: init_app,
deps: [AppConfig, ConfigurationService, LoggerService],
multi: false
}
],
bootstrap: [AppComponent, appRoutingProviders]
})
export class AppModule
{
constructor(private httpModule: HttpModule)
{
}
}
最佳答案
我找到了这个问题的解决方案。帖子里的答案angular 2 - Injected service in http error handler给我指明了正确的方向。我正在使用以下内容:
.map(this.dataHandler.extractData)
.catch(this.dataHandler.handleHttpError);
但应该使用:
.map(res => this.dataHandler.extractData(res))
.catch(err => this.dataHandler.handleHttpError(err));
出于某种原因需要 lambda。
关于Angular 2注入(inject)服务未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956852/
我正在尝试测试依赖于其他服务 authService 的服务 documentViewer angular .module('someModule') .service('docu
如果我的网站上线(不要认为它会,目前它只是一个学习练习)。 我一直在使用 mysql_real_escape_string();来自 POST、SERVER 和 GET 的数据。另外,我一直在使用 i
我有以下代码,它容易受到 SQL 注入(inject)的攻击(我认为?): $IDquery = mysqli_query($connection, "SELECT `ID` FROM users W
我一直在自学如何创建扩展,以期将它们用于 CSS 注入(inject)(以及最终以 CSS 为载体的 SVG 注入(inject),但那是以后的问题)。 这是我当前的代码: list .json {
这个简单的代码应该通过 Java Spring 实现一个简单的工厂。然而结果是空指针,因为 Human 对象没有被注入(inject)对象(所以它保持空)。 我做错了什么? 谢谢 配置 @Config
我正在编写一个 ASP.NET MVC4 应用程序,它最终会动态构建一个 SQL SELECT 语句,以便稍后存储和执行。动态 SQL 的结构由用户配置以用户友好的方式确定,具有标准复选框、下拉列表和
首先让我说我是我为确保 SQL 注入(inject)攻击失败而采取的措施的知己。所有 SQL 查询值都是通过事件记录准备语句完成的,所有运算符(如果不是硬编码)都是通过数字白名单系统完成的。这意味着如
这是 SQL 映射声称可注入(inject)的负载: user=-5305' UNION ALL SELECT NULL,CONCAT(0x716b6b7071,0x4f5577454f76734
我正在使用 Kotlin 和 Android 架构组件(ViewModel、LiveData)构建一个新的 Android 应用程序的架构,并且我还使用 Koin 作为我的依赖注入(inject)提供
假设 RequestScope 处于 Activity 状态(使用 cdi-unit 的 @InRequestScope) 给定 package at.joma.stackoverflow.cdi;
我有一个搜索表单,可以在不同的提供商中搜索。 我从拥有一个基本 Controller 开始 public SearchController : Controller { protected r
SQLite 注入 如果您的站点允许用户通过网页输入,并将输入内容插入到 SQLite 数据库中,这个时候您就面临着一个被称为 SQL 注入的安全问题。本章节将向您讲解如何防止这种情况的发生,确保脚
我可以从什么 dll 中获得 Intercept 的扩展?我从 http://github.com/danielmarbach/ninject.extensions.interception 添加了
使用 NInject 解析具有多个构造函数的类似乎不起作用。 public class Class1 : IClass { public Class1(int param) {...} public
我有一个 MetaManager 类: @Injectable() export class MetaManager{ constructor(private handlers:Handler
我是 Angular 的新手,我不太清楚依赖注入(inject)是如何工作的。我的问题是我有依赖于服务 B 的服务 A,但是当我将服务 A 注入(inject)我的测试服务 B 时,服务 B 变得未定
我正在为我的项目使用 android 应用程序启动、刀柄和空间。我在尝试排队工作时遇到错误: com.test E/WM-WorkerFactory: Could not instantiate co
我不确定这是什么糖语法,但让我向您展示问题所在。 def factors num (1..num).select {|n| num % n == 0} end def mutual_factors
简单的问题,我已经看过这个了:Managing imports in Scalaz7 ,但我不知道如何最小化注入(inject) right和 left方法到我的对象中以构造 \/ 的实例. 我确实尝
在我的 Aurelia SPA 中,我有一些我想在不同模块中使用的功能。它依赖于调用时给出的参数和单例的参数。有没有办法创建一个导出函数,我可以将我的 Auth 单例注入(inject)其中,而不必在
我是一名优秀的程序员,十分优秀!