Service injection aliasing with useClass and useExisting
As your application becomes more complex, you may come to a situation where you would like to use your services in a polymorphic style. More specifically, some places in your application may want to request Service A, but a configuration somewhere in your application will actually give it Service B. This recipe will demonstrate one way in which this can be useful, but this behavior allows your application to be more extensible in multiple ways.
Note
The code, links, and a live example of this are available at http://ngcookbook.herokuapp.com/1109/.
Getting ready
Suppose you begin with the following skeleton application.
Dual services
You begin with two services, ArticleService
and EditorArticleService
, and their shared interface, ArticleSourceInterface
. EditorArticleService
inherits from ArticleService
:
[app/article-source.interface.ts] export interface ArticleSourceInterface { getArticle():Article } ...