[英]How to register service in providers with dependencies?
I have custom service RequestsMethods
in Angular 5: 我在Angular 5中有自定义服务
RequestsMethods
:
@Injectable()
export class RequestsMethods {
constructor(private http: HttpService) {
}
}
Where HttpService
is: HttpService
在哪里:
import {Headers, Http, Request, RequestOptions, RequestOptionsArgs, Response, XHRBackend} from '@angular/http';
@Injectable()
export class HttpService extends Http {
constructor(private router: Router, backend: XHRBackend, options: RequestOptions) {
super(backend, options);
}
}
My ngModule
is: 我的
ngModule
是:
providers: [
BrowserXhr,
XHRBackend,
RequestsMethods,
FormRegister
],
I get the following error: 我收到以下错误:
ERROR Error: StaticInjectorError(AppModule)[RequestsMethods -> HttpService]: StaticInjectorError(Platform: core)[RequestsMethods -> HttpService]: NullInjectorError: No provider for HttpService!
错误错误:StaticInjectorError(AppModule)[RequestsMethods-> HttpService]:StaticInjectorError(Platform:core)[RequestsMethods-> HttpService]:NullInjectorError:HttpService没有提供者! at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.ge
在NullInjector.push ../ node_modules/@angular/core/fesm5/core.js.NullInjector.ge
From error message I got that I need register HttpService
in providers, I tried: 从错误消息中得知,我需要在提供程序中注册
HttpService
,我尝试了:
providers: [
HttpService,
BrowserXhr,
XHRBackend,
RequestsMethods,
FormRegister
],
Now I have another error: 现在我有另一个错误:
ERROR Error: StaticInjectorError(AppModule)[XHRBackend -> ResponseOptions]:
StaticInjectorError(Platform: core)[XHRBackend -> ResponseOptions]:
NullInjectorError: No provider for ResponseOptions!
Now my provider section is: 现在我的提供者部分是:
providers: [
{
provide: RequestsMethods ,
useFactory: RequestsMethodsFactory,
deps: [HttpService]
},
FormRegister,
ErrorWatcher,
{
provide: HttpService,
useFactory: httpFactoryService,
deps: [Router, XHRBackend, RequestOptions]
}
],
My last changes in ngModule file: 我对ngModule文件的最后更改:
export function httpFactoryService(router: Router, backend: XHRBackend, options: RequestOptions) {
return new HttpService(router, backend, options);
}
function RequestsMethodsFactory(router: Router, backend: XHRBackend, options: RequestOptions) {
return new RequestsMethods(httpFactoryService(router, backend, options));
}
// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
providers: [
{
provide: RequestsMethods ,
useFactory: RequestsMethodsFactory,
deps: [Router, XHRBackend, RequestOptions]
},
FormRegister,
ErrorWatcher,
{
provide: HttpService,
useFactory: httpFactoryService,
deps: [Router, XHRBackend, RequestOptions]
}
],
function RequestsMethodsFactory(HttpService) {
return new RequestsMethods(new HttpService());
}
@NgModule({
declarations: [
AppComponent,
...
],
imports: [
BrowserModule,
HttpModule
....
],
providers: [
{
provide: RequestsMethods ,
useFactory: RequestsMethodsFactory,
deps: [HttpService]
},
....
],
],
bootstrap: [AppComponent]
})
export class AppModule { }
Read from the documentation: https://angular.io/guide/dependency-injection#factory-providers 阅读文档: https : //angular.io/guide/dependency-injection#factory-providers
Your HttpService
injects through the constructor three major tokens. 您的
HttpService
通过构造函数注入了三个主要标记。 Two of them ( XHRBackend
and RequestOptions
) belong in the HttpModule
(deprecated as for Angular 4.2 - you should use the types contained in HttpClientModule
instead). 其中两个(
XHRBackend
和RequestOptions
)属于HttpModule
(不推荐使用Angular 4.2-您应使用HttpClientModule
包含的类型)。 The other one is Router
, which belongs in RouterModule
. 另一个是
Router
,它属于RouterModule
。
In order for your Ngmodule to be aware of the types and tokens required in the cnstructor, you will want to declare both RouterModule
and HttpModule
in the imports
property of your Ngmodule
: 为了让您的Ngmodule要知道在cnstructor所需的类型和令牌,你将要同时声明
RouterModule
和HttpModule
的imports
你的财产Ngmodule
:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule, // <-- This!
HttpModule // <-- And this!
],
providers: [
HttpService,
RequestsMethods,
],
bootstrap: [AppComponent]
})
export class AppModule { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.