如何在具有依赖项的提供者中注册服务?

时间:2018-08-29 20:28:30

标签: angular angular5

我在Angular 5中拥有自定义服务RequestsMethods

@Injectable()
export class RequestsMethods {
  constructor(private http: HttpService) {
  }
}

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);
    }
}

我的ngModule是:

providers: [
    BrowserXhr,
    XHRBackend,
    RequestsMethods,
    FormRegister
  ],

我收到以下错误:

  

错误错误:StaticInjectorError(AppModule)[RequestsMethods->   HttpService]:StaticInjectorError(平台:核心)[RequestsMethods   -> HttpService]:       NullInjectorError:没有HttpService的提供程序!       在NullInjector.push ../ node_modules/@angular/core/fesm5/core.js.NullInjector.ge

从错误消息中得知,我需要在提供程序中注册HttpService,我尝试过:

providers: [
    HttpService,
    BrowserXhr,
    XHRBackend,
    RequestsMethods,
    FormRegister
  ], 

现在我还有另一个错误:

ERROR Error: StaticInjectorError(AppModule)[XHRBackend -> ResponseOptions]: 
  StaticInjectorError(Platform: core)[XHRBackend -> ResponseOptions]: 
    NullInjectorError: No provider for ResponseOptions!

现在我的提供者部分是:

 providers: [
    {
      provide: RequestsMethods ,
      useFactory: RequestsMethodsFactory,
      deps: [HttpService]
    },
    FormRegister,
    ErrorWatcher,
    {
      provide: HttpService,
      useFactory: httpFactoryService,
      deps: [Router, XHRBackend, RequestOptions]
    }
  ],

我对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]
    }
  ],

2 个答案:

答案 0 :(得分:1)

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 { }

从文档中阅读:https://angular.io/guide/dependency-injection#factory-providers

答案 1 :(得分:1)

您的HttpService通过构造函数注入三个主要标记。其中两个(XHRBackendRequestOptions属于HttpModule(已从Angular 4.2中弃用-您应改用HttpClientModule中包含的类型)。另一个是Router,属于RouterModule

为了使您的Ngmodule知道cnstructor中所需的类型和令牌,您将需要在{{RouterModule}属性中声明HttpModuleimports 1}}:

Ngmodule