October 2017
Intermediate to advanced
280 pages
6h 50m
English
Another way to proceed is using the useExisting property of the provider's configuration object:
@NgModule({ // ... providers: [
DummyHttp,
{ provide: Http, useExisting: DummyHttp },
UserService
]})class TestingModule {}
In the preceding snippet, we register providers for three tokens: DummyHttp, UserService, and Http. We declare that we want to bind the Http token to the existing token, DummyHttp. This means that, when the Http service is requested, the injector will find the provider for the token used as the value of the useExisting property and instantiate it or get the value associated with it (in case it was already instantiated). We can think of useExisting as creating an alias of the given token:
// ch6/configuring-providers/existing/app.ts ...
Read now
Unlock full access