read-only
Binds a DI token to an existing token.
Injector
returns the same instance as if the provided token was used.
This is in contrast to useClass where a separate instance of useClass
is returned.
Because useExisting and useClass are often confused the example contains both use cases for easy comparison.
class Vehicle {}
class Car extends Vehicle {}
var injectorAlias = Injector.resolveAndCreate([
Car,
new Provider(Vehicle, useExisting: Car)
]);
var injectorClass = Injector.resolveAndCreate([
Car,
new Provider(Vehicle, useClass: Car)
]);
expect(injectorAlias.get(Vehicle) == injectorAlias.get(Car), isTrue);
expect(injectorAlias.get(Vehicle) is Car, isTrue);
expect(injectorClass.get(Vehicle) != injectorClass.get(Car), isTrue);
expect(injectorClass.get(Vehicle) is Car, isTrue);