Providing alternate classes against the same DI Token
In this recipe, you’ll learn how to provide two different services to the app using Aliased class providers. This is extremely helpful in complex applications where you need to narrow down the implementation of the service/class for some components/modules, that is, providing different classes against the same DI token to have a polymorphic behavior. Additionally, aliasing is used in component/service unit tests to mock the dependent service’s actual implementation so that we don’t rely on it.
Getting ready
The app that we are going to work with resides in start/apps/chapter03/ng-aliased-class-providers
, inside the cloned repository:
- Open the code repository in your code editor.
- Open the Terminal, navigate to the code repository directory, and run the following command to serve the project:
npm run serve ng-aliased-class-providers
This should open the app in a new...