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
angular typescript


source to share


No one has answered this question yet

See similar questions:

23
How to extend angular 2 http class in angular 2 final
4
How can I provide inexperienced development (not testing) services in Angular 2?

or similar:

57
Inheritance and dependency
18
Is it possible to inject an interface using angular2?
eleven
Testing an Angular2 service with a mock backend
6
How to pass parameters for dependencies during service injection in services in Angular 2 (Ionic 2 / Angular 2 / Typescript)
1
Service injection issues
1
How can I extend any core directive, service, Angular2 class?
1
How to make Angular2 Service Singleton .... again
0
Isolated unit tests with vendor dependencies in Angular2 final
0
How can I create an Http instance using ReflectiveInjector?
0
Angular2 Http extension does not provide service



All Articles
Loading...
X
Show
Funny
Dev
Pics