Skip to Content
GuidesBinding

Binding

A binding connects an identifier to a dependency item. The injector uses bindings to know what to provide when a dependency is requested.

Registering Bindings

In the Constructor

The most common way is to pass bindings when creating the injector:

const injector = new Injector([ [AuthService], // Class as both identifier and item [ILogger, { useClass: ConsoleLogger }], // Interface → Class [IConfig, { useValue: { debug: true } }], // Interface → Value [IFactory, { useFactory: () => create() }], // Interface → Factory ]);

Using the add Method

You can also add bindings later:

const injector = new Injector(); injector.add([AuthService]); injector.add([ILogger, { useClass: ConsoleLogger }]);

Be careful with add — make sure bindings are registered before they are requested, or you’ll get an error.

Modifying Bindings

Replace a Binding

injector.replace([ILogger, { useClass: FileLogger }]);

Delete a Binding

injector.delete(ILogger);

You can only replace or delete bindings before they are resolved. Once a dependency has been created, changes won’t take effect.

Last updated on