Angular 注入服务到服务

Angular 注入服务到服务,angular,typescript,dependency-injection,angular-services,Angular,Typescript,Dependency Injection,Angular Services,我通过VisualStudio模板构建了一个Angular应用程序 给出了以下结构: /Clientapp ./app/app.module.shared.ts ./app/app.module.client.ts ./app/app.module.server.ts /部件/* ./services/person-data.service.ts ./services/auth-http.service.ts ./boot-client.ts ./boot-server.ts 因此,在per

我通过VisualStudio模板构建了一个Angular应用程序

给出了以下结构:

  • /Clientapp
  • ./app/app.module.shared.ts
  • ./app/app.module.client.ts
  • ./app/app.module.server.ts
  • /部件/*
  • ./services/person-data.service.ts
  • ./services/auth-http.service.ts
  • ./boot-client.ts
  • ./boot-server.ts
因此,在person-data.service.ts中,我想使用auth-http.service.ts

person-data.service.ts

从“../models/Person”导入{Person}
从“../constants/global.constants”导入{Configuration};
从“@angular/core”导入{Injectable,injection};
从'@angular/Http'导入{Http,Response,Headers};
导入'rxjs/add/operator/map';
从“rxjs/Observable”导入{Observable};
从“../services/auth http.service”导入{AuthHttpService};
@可注射()
导出类PersonService{
构造函数(私有http:http、@Inject(AuthHttpService)私有authHttp:AuthHttpService){
this.actionUrl=Configuration.API_SERVER+'API/person/';
this.headers=新的headers();
this.headers.append('Content-Type','application/json');
append('Accept','application/json');
}
公共GetAll=():可观察=>{
返回this.authHttp.get(this.actionUrl).map((response:response)=>response.json());
}
}
auth-http.service.ts

从'@angular/core'导入{Injectable,injection};
从'@angular/Http'导入{Http,Response,RequestOptions};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/map';
导入“rxjs/add/operator/catch”;
从“/auth.service”导入{AuthService};
@可注射()
导出类AuthHttpService{
构造函数(私有http:http、@Inject(AuthService)私有AuthService:AuthService){
}
获取(url:string,options?:RequestOptions):可观察{
log(“AuthHttpService获取:+url”);
如果(选项){
选项=this.authService.\u setRequestOptions(选项);
}否则{
选项=this.authService.\u setRequestOptions();
}
返回this.http.get(url,选项);
}
}
app.module.shared.ts

从'@angular/core'导入{NgModule};
从'@angular/router'导入{RouterModule};
从“./services/person data.service”导入{PersonService}
从“./constants/global.constants”导入{Configuration}
从“./services/auth.service”导入{AuthService}
从“./services/auth http.service”导入{AuthHttpService}
从“./components/app/app.component”导入{AppComponent}
导出常量sharedConfig:NgModule={
引导:[AppComponent],
声明:[
应用组件
],
供应商:[
AuthHttpService,
配置
个人服务,
授权服务
],
进口:[
RouterModule.forRoot([
{路径:'',重定向到:'home',路径匹配:'full'},
{路径:'**',重定向到:'home'}
])
]
};
app.module.client.ts

从'@angular/core'导入{NgModule};
从“@angular/platform browser”导入{BrowserModule};
从'@angular/forms'导入{FormsModule};
从'@angular/http'导入{HttpModule};
从“@angular/platform browser/animations”导入{BrowserAnimationsModule};
从“./app.module.shared”导入{sharedConfig};
@NGD模块({
引导:sharedConfig.bootstrap,
声明:sharedConfig.declarations,
进口:[
浏览器模块,
FormsModule,
HttpModule,
BrowserAnimationsModule,
…sharedConfig.imports
],
供应商:[
{提供:'ORIGIN_URL',useValue:location.ORIGIN}
]
})
导出类AppModule{
}
当我运行应用程序时,我得到以下错误

处理请求时发生未处理的异常。 异常:调用节点模块失败,错误:错误:无提供程序 用于AuthHttpService

我错过了什么?

我想你忘了

app.module.client.ts中导入
sharedConfig.providers

app.module.client.ts

...
providers: [
        ...sharedConfig.providers,
        { provide: 'ORIGIN_URL', useValue: location.origin }
    ]
...

尝试从构造函数中删除注入装饰器,因为它们不是必需的

然后,如错误所示,在模块的提供程序内部导入,如:

providers: [
             AuthHttpService,
             // Other services here,
             { provide: 'ORIGIN_URL', useValue: location.origin }
            ]

请分享app.module.ts/app.module.shared.ts好吗?编辑了主帖子。在app.module.server.ts中,@angular/platform server的ServerModule被导入
@Inject(AuthHttpService)
,而
@Inject(AuthHttpService)私有authHttp:AuthHttpService
是冗余的。只有当
@Inject(…)
的参数与参数类型不同时,才需要它。(与“@Inject(AuthService)”相同)非常感谢。。很简单,但不知怎的,我搞不懂。