|
1 | | -# Injection context |
| 1 | +# インジェクションコンテキスト |
2 | 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. |
| 3 | +依存性の注入 (DI) システムは、現在のインジェクターが使用可能なランタイムコンテキストに、内部的に依存します。 |
| 4 | +つまり、インジェクターはこのコンテキストでコードが実行される場合にのみ、機能します。 |
5 | 5 |
|
6 | | -The injection context is available in these situations: |
| 6 | +インジェクションコンテキストは、次のような場合に利用可能です。 |
7 | 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. |
| 8 | +* `@Injectable`や`@Component`など、DIシステムによってインスタンス化されるクラスの(`constructor`を介した)構築 |
| 9 | +* このようなクラスのフィールドの初期化子内 |
| 10 | +* `Provider`または`@Injectable`の`useFactory`に指定されたファクトリ関数内 |
| 11 | +* `InjectionToken`に指定された`factory`関数内 |
| 12 | +* インジェクションコンテキストで実行されるスタックフレーム内 |
13 | 13 |
|
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)関数を使用してインスタンスを注入できるようになります。 |
15 | 15 |
|
16 | | -## Class constructors |
| 16 | +## クラスコンストラクター |
17 | 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. |
| 18 | +DIシステムはクラスをインスタンス化するたびに、インジェクションコンテキストでインスタンス化を行います。これはフレームワーク自体によって処理されます。クラスのコンストラクターはそのランタイムコンテキストで実行されるため、[`inject`](api/core/inject)関数を使用してトークンを注入できます。 |
19 | 19 |
|
20 | 20 | <code-example language="typescript"> |
21 | 21 | class MyComponent { |
22 | 22 | private service1: Service1; |
23 | | - private service2: Service2 = inject(Service2); // In context |
| 23 | + private service2: Service2 = inject(Service2); // コンテキスト内 |
24 | 24 |
|
25 | 25 | constructor() { |
26 | | - this.service1 = inject(HeroService) // In context |
| 26 | + this.service1 = inject(HeroService) // コンテキスト内 |
27 | 27 | } |
28 | 28 | } |
29 | 29 | </code-example> |
30 | 30 |
|
31 | | -## Stack frame in context |
| 31 | +## コンテキスト内のスタックフレーム |
32 | 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. |
| 33 | +一部のAPIは、インジェクションコンテキストで実行されるように設計されています。これは、たとえばルーターガードの場合に当てはまります。これにより、[`inject`](api/core/inject)関数を使用して、ガード関数内でサービスにアクセスできるようになります。 |
34 | 34 |
|
35 | | -Here is an example for `CanActivateFn` |
| 35 | +`CanActivateFn`の場合の例 |
36 | 36 | <code-example format="typescript" language="typescript"> |
37 | 37 | const canActivateTeam: CanActivateFn = |
38 | 38 | (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => { |
39 | 39 | return inject(PermissionsService).canActivate(inject(UserToken), route.params.id); |
40 | 40 | }; |
41 | 41 | </code-example> |
42 | 42 |
|
43 | | -## Run within an injection context |
| 43 | +## インジェクションコンテキスト内で実行する |
44 | 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. |
| 45 | +インジェクションコンテキストに属さない特定の関数を、インジェクションコンテキスト内で実行したい場合は、`runInInjectionContext`を使用して実行できます。 |
| 46 | +これには、たとえば`EnvironmentInjector`などの特定のインジェクターにアクセスできる必要があります。 |
47 | 47 |
|
48 | 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 | 49 | </code-example> |
50 | 50 |
|
51 | | -Note that `inject` will return an instance only if the injector can resolve the required token. |
| 51 | +`inject`は、インジェクターが必要なトークンを解決できる場合にのみインスタンスを返すことに注意してください。 |
52 | 52 |
|
53 | | -## Asserts the context |
| 53 | +## コンテキストの検証 |
54 | 54 |
|
55 | | -Angular provides `assertInInjectionContext` helper function to assert that the current context is an injection context. |
| 55 | +Angularは、現在のコンテキストがインジェクションコンテキストであることを検証するための`assertInInjectionContext`ヘルパー関数を提供します。 |
56 | 56 |
|
57 | | -## Using DI outside of a context |
| 57 | +## コンテキスト外でのDIの使用 |
58 | 58 |
|
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)がスローされます。 |
60 | 60 |
|
61 | | - |
62 | | - |
63 | | -@reviewed 2023-04-11 |
| 61 | +@reviewed 2023-04-11 |
0 commit comments