Skip to content

Commit e6d6c2b

Browse files
translate: guide/dependency-injection-context (#887) (#898)
* translate: guide/dependency-injection-context (#887) * fix: Apply suggestions from code review アサート→検証 Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com> --------- Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
1 parent ac8f949 commit e6d6c2b

File tree

2 files changed

+89
-28
lines changed

2 files changed

+89
-28
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Injection context
2+
3+
The dependency injection (DI) system relies internaly on a runtime context where the current injector is available.
4+
This means that injectors can only work when code is executed in this context.
5+
6+
The injection context is available in these situations:
7+
8+
* Construction (via the `constructor`) of a class being instantiated by the DI system, such as an `@Injectable` or `@Component`.
9+
* In the initializer for fields of such classes.
10+
* In the factory function specified for `useFactory` of a `Provider` or an `@Injectable`.
11+
* In the `factory` function specified for an `InjectionToken`.
12+
* Within a stack frame that is run in a injection context.
13+
14+
Knowing when your are in an injection context, will allow you to use the [`inject`](api/core/inject) function to inject instances.
15+
16+
## Class constructors
17+
18+
Everytime the DI system instantiates a class, this is done in an injection context. This is being handled by the framework itself. The constructor of the class is executed in that runtime context thus allowing to inject a token using the [`inject`](api/core/inject) function.
19+
20+
<code-example language="typescript">
21+
class MyComponent {
22+
private service1: Service1;
23+
private service2: Service2 = inject(Service2); // In context
24+
25+
constructor() {
26+
this.service1 = inject(HeroService) // In context
27+
}
28+
}
29+
</code-example>
30+
31+
## Stack frame in context
32+
33+
Some APIs are designed to be run in an injection context. This is the case, for example, of the router guards. It allows the use of [`inject`](api/core/inject) to access a service within the guard function.
34+
35+
Here is an example for `CanActivateFn`
36+
<code-example format="typescript" language="typescript">
37+
const canActivateTeam: CanActivateFn =
38+
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
39+
return inject(PermissionsService).canActivate(inject(UserToken), route.params.id);
40+
};
41+
</code-example>
42+
43+
## Run within an injection context
44+
45+
When you want to run a given function in an injection context without being in one, you can do it with `runInInjectionContext`.
46+
This requires to have access to a given injector like the `EnvironmentInjector` for example.
47+
48+
<code-example path="dependency-injection/src/app/heroes/hero.service.5.ts" region="run-in-context" header="src/app/heroes/hero.service.ts">
49+
</code-example>
50+
51+
Note that `inject` will return an instance only if the injector can resolve the required token.
52+
53+
## Asserts the context
54+
55+
Angular provides `assertInInjectionContext` helper function to assert that the current context is an injection context.
56+
57+
## Using DI outside of a context
58+
59+
Calling [`inject`](api/core/inject) or calling `assertInInjectionContext` outside of an injection context will throw [error NG0203](/errors/NG0203).
60+
61+
62+
63+
@reviewed 2023-04-11
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
1-
# Injection context
1+
# インジェクションコンテキスト
22

3-
The dependency injection (DI) system relies internaly on a runtime context where the current injector is available.
4-
This means that injectors can only work when code is executed in this context.
3+
依存性の注入 (DI) システムは、現在のインジェクターが使用可能なランタイムコンテキストに、内部的に依存します。
4+
つまり、インジェクターはこのコンテキストでコードが実行される場合にのみ、機能します。
55

6-
The injection context is available in these situations:
6+
インジェクションコンテキストは、次のような場合に利用可能です。
77

8-
* Construction (via the `constructor`) of a class being instantiated by the DI system, such as an `@Injectable` or `@Component`.
9-
* In the initializer for fields of such classes.
10-
* In the factory function specified for `useFactory` of a `Provider` or an `@Injectable`.
11-
* In the `factory` function specified for an `InjectionToken`.
12-
* Within a stack frame that is run in a injection context.
8+
* `@Injectable``@Component`など、DIシステムによってインスタンス化されるクラスの(`constructor`を介した)構築
9+
* このようなクラスのフィールドの初期化子内
10+
* `Provider`または`@Injectable``useFactory`に指定されたファクトリ関数内
11+
* `InjectionToken`に指定された`factory`関数内
12+
* インジェクションコンテキストで実行されるスタックフレーム内
1313

14-
Knowing when your are in an injection context, will allow you to use the [`inject`](api/core/inject) function to inject instances.
14+
どのような時にインジェクションコンテキスト内にいるかを知ることで、[`inject`](api/core/inject)関数を使用してインスタンスを注入できるようになります。
1515

16-
## Class constructors
16+
## クラスコンストラクター
1717

18-
Everytime the DI system instantiates a class, this is done in an injection context. This is being handled by the framework itself. The constructor of the class is executed in that runtime context thus allowing to inject a token using the [`inject`](api/core/inject) function.
18+
DIシステムはクラスをインスタンス化するたびに、インジェクションコンテキストでインスタンス化を行います。これはフレームワーク自体によって処理されます。クラスのコンストラクターはそのランタイムコンテキストで実行されるため、[`inject`](api/core/inject)関数を使用してトークンを注入できます。
1919

2020
<code-example language="typescript">
2121
class MyComponent {
2222
private service1: Service1;
23-
private service2: Service2 = inject(Service2); // In context
23+
private service2: Service2 = inject(Service2); // コンテキスト内
2424

2525
constructor() {
26-
this.service1 = inject(HeroService) // In context
26+
this.service1 = inject(HeroService) // コンテキスト内
2727
}
2828
}
2929
</code-example>
3030

31-
## Stack frame in context
31+
## コンテキスト内のスタックフレーム
3232

33-
Some APIs are designed to be run in an injection context. This is the case, for example, of the router guards. It allows the use of [`inject`](api/core/inject) to access a service within the guard function.
33+
一部のAPIは、インジェクションコンテキストで実行されるように設計されています。これは、たとえばルーターガードの場合に当てはまります。これにより、[`inject`](api/core/inject)関数を使用して、ガード関数内でサービスにアクセスできるようになります。
3434

35-
Here is an example for `CanActivateFn`
35+
`CanActivateFn`の場合の例
3636
<code-example format="typescript" language="typescript">
3737
const canActivateTeam: CanActivateFn =
3838
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
3939
return inject(PermissionsService).canActivate(inject(UserToken), route.params.id);
4040
};
4141
</code-example>
4242

43-
## Run within an injection context
43+
## インジェクションコンテキスト内で実行する
4444

45-
When you want to run a given function in an injection context without being in one, you can do it with `runInInjectionContext`.
46-
This requires to have access to a given injector like the `EnvironmentInjector` for example.
45+
インジェクションコンテキストに属さない特定の関数を、インジェクションコンテキスト内で実行したい場合は、`runInInjectionContext`を使用して実行できます。
46+
これには、たとえば`EnvironmentInjector`などの特定のインジェクターにアクセスできる必要があります。
4747

4848
<code-example path="dependency-injection/src/app/heroes/hero.service.5.ts" region="run-in-context" header="src/app/heroes/hero.service.ts">
4949
</code-example>
5050

51-
Note that `inject` will return an instance only if the injector can resolve the required token.
51+
`inject`は、インジェクターが必要なトークンを解決できる場合にのみインスタンスを返すことに注意してください。
5252

53-
## Asserts the context
53+
## コンテキストの検証
5454

55-
Angular provides `assertInInjectionContext` helper function to assert that the current context is an injection context.
55+
Angularは、現在のコンテキストがインジェクションコンテキストであることを検証するための`assertInInjectionContext`ヘルパー関数を提供します。
5656

57-
## Using DI outside of a context
57+
## コンテキスト外でのDIの使用
5858

59-
Calling [`inject`](api/core/inject) or calling `assertInInjectionContext` outside of an injection context will throw [error NG0203](/errors/NG0203).
59+
インジェクションコンテキストの外で、[`inject`](api/core/inject)`assertInInjectionContext`を呼び出した場合、[error NG0203](/errors/NG0203)がスローされます。
6060

61-
62-
63-
@reviewed 2023-04-11
61+
@reviewed 2023-04-11

0 commit comments

Comments
 (0)