You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After declaring identifiers and dependencies, it is necessary to establish a binding relationship between the identifiers and dependencies, and register these bindings in the injector. The injector can only return dependencies based on identifiers when it knows a set of bindings.
3
+
# Binding
4
4
5
-
There are two ways to register bindings on the injector:
5
+
A binding connects an identifier to a dependency item. The injector uses bindings to know what to provide when a dependency is requested.
6
6
7
-
- As parameters of the injector constructor
8
-
- By calling the `add` method of the injector
7
+
## Registering Bindings
9
8
10
-
##As parameters of the injector constructor
9
+
### In the Constructor
11
10
12
-
The first parameter of the `Injector` constructor is a set of bindings. You have seen a lot of code like this in the previous section:
11
+
The most common way is to pass bindings when creating the injector:
13
12
14
13
```ts
15
14
const injector =newInjector([
16
-
[SomeClass],
17
-
[IdentifierA, { useClass: SomeClass }],
18
-
[IdentifierB, { useValue: SomeValue }],
19
-
[IdentifierC, { useFactory: SomeFactory }],
20
-
[IdentifierD, { useAsync: SomeAsyncFunction }],
15
+
[AuthService], // Class as both identifier and item
16
+
[ILogger, { useClass: ConsoleLogger }], // Interface → Class
This approach is more flexible because it does not restrict when you can register bindings. However, this type of binding can be a double-edged sword as it may not have been registered by the time you retrieve dependencies through the injector. Therefore, we recommend using the first method as a priority unless you can ensure the correct order of registration and retrieval.
45
+
### Delete a Binding
33
46
34
-
## Replacing or deleting bindings
47
+
```ts
48
+
injector.delete(ILogger);
49
+
```
35
50
36
-
Before an identifier is resolved, it is possible to replace or delete the binding of the identifier. This can be done by calling the `replace` or `delete` methods of the injector.
51
+
<Callouttype="warning">
52
+
You can only replace or delete bindings **before** they are resolved. Once a dependency has been created, changes won't take effect.
Copy file name to clipboardExpand all lines: docs/content/docs/declare-dependency.mdx
+33-47Lines changed: 33 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,90 +2,75 @@ import { Callout } from "nextra/components";
2
2
3
3
# Declare Dependency
4
4
5
-
After creating identifiers and dependency items, you need to declare dependency relationship among dependencies. There are two types of dependency items that could dependent on others: class items and factory function items.
5
+
Once you have identifiers and dependency items, you need to declare the relationships between them.
6
6
7
-
## On a class
7
+
## On a Class
8
8
9
-
To declare dependencies of a class, you could use the `Injector` decorator:
9
+
Use the `@Inject` decorator on constructor parameters:
0 commit comments