Angular2 Extend Service

I want to extend an existing angular service (in this example Http

).

Requirements:

  • Service Extension is handled through Angle Dependency Injection
  • It should be possible to renew the service several times (for example, as the Decorator Pattern allows )
  • The generated object should implement (if possible) or extend Http

    so that existing code does not need to be changed. DI introduces "decorated" Http

    .
  • One possible use case would be Http

    "decorated" SecureHttp

    and LoggingHttp

    .

I'm tired so far:

  • SecureHttp implements Http

    doesn't seem to work because 1.is Http

    not an interface and 2.it is protected by members that need to be implemented
  • Extend Http

    class SecureHttp extends Http {
      constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private _http: Http){
        super(_backend, _defaultOptions);
      }
    
      get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        console.log("This is secure Http.");
        return this._http.get(url, options);
      }
    ...
    }
    
          

    But I don't know how to provide the service, so it Http

    is injected by the class SecureHttp

    , but Http

    in it SecureHttp

    gets injected Http

    .

Am I on the right track?

Or are there other concepts in angular2 that allow you to extend existing services that I am not aware of?

+3


source to share





All Articles