Skip to content

Commit fddad4d

Browse files
committed
feat(docs): translate guide/di/creating-and-using-services to Japanese
1 parent aac3960 commit fddad4d

File tree

2 files changed

+129
-24
lines changed

2 files changed

+129
-24
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Creating and using services
2+
3+
Services are reusable pieces of code that can be shared across your Angular application. They typically handle data fetching, business logic, or other functionality that multiple components need to access.
4+
5+
## Creating a service
6+
7+
You can create a service with the [Angular CLI](tools/cli) with the following command:
8+
9+
```bash
10+
ng generate service CUSTOM_NAME
11+
```
12+
13+
This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory.
14+
15+
You can also manually create a service by adding the `@Injectable()` decorator to a TypeScript class. This tells Angular that the service can be injected as a dependency.
16+
17+
Here is an example of a service that allows users to add and request data:
18+
19+
```ts
20+
// 📄 src/app/basic-data-store.ts
21+
import { Injectable } from '@angular/core';
22+
23+
@Injectable({ providedIn: 'root' })
24+
export class BasicDataStore {
25+
private data: string[] = []
26+
27+
addData(item: string): void {
28+
this.data.push(item)
29+
}
30+
31+
getData(): string[] {
32+
return [...this.data]
33+
}
34+
}
35+
```
36+
37+
## How services become available
38+
39+
When you use `@Injectable({ providedIn: 'root' })` in your service, Angular:
40+
41+
- **Creates a single instance** (singleton) for your entire application
42+
- **Makes it available everywhere** without any additional configuration
43+
- **Enables tree-shaking** so the service is only included in your JavaScript bundle if it's actually used
44+
45+
This is the recommended approach for most services.
46+
47+
## Injecting a service
48+
49+
Once you've created a service with `providedIn: 'root'`, you can inject it anywhere in your application using the `inject()` function from `@angular/core`.
50+
51+
### Injecting into a component
52+
53+
```angular-ts
54+
import { Component, inject } from '@angular/core';
55+
import { BasicDataStore } from './basic-data-store';
56+
57+
@Component({
58+
selector: 'app-example',
59+
template: `
60+
<div>
61+
<p>{{ dataStore.getData() }}</p>
62+
<button (click)="dataStore.addData('More data')">
63+
Add more data
64+
</button>
65+
</div>
66+
`
67+
})
68+
export class ExampleComponent {
69+
dataStore = inject(BasicDataStore);
70+
}
71+
```
72+
73+
### Injecting into another service
74+
75+
```ts
76+
import { inject, Injectable } from '@angular/core';
77+
import { AdvancedDataStore } from './advanced-data-store';
78+
79+
@Injectable({
80+
providedIn: 'root',
81+
})
82+
export class BasicDataStore {
83+
private advancedDataStore = inject(AdvancedDataStore);
84+
private data: string[] = [];
85+
86+
addData(item: string): void {
87+
this.data.push(item);
88+
}
89+
90+
getData(): string[] {
91+
return [...this.data, ...this.advancedDataStore.getData()];
92+
}
93+
}
94+
```
95+
96+
## Next steps
97+
98+
While `providedIn: 'root'` covers most use cases, Angular offers additional ways to provide services for specialized scenarios:
99+
100+
- **Component-specific instances** - When components need their own isolated service instances
101+
- **Manual configuration** - For services that require runtime configuration
102+
- **Factory providers** - For dynamic service creation based on runtime conditions
103+
- **Value providers** - For providing configuration objects or constants
104+
105+
You can learn more about these advanced patterns in the next guide: [defining dependency providers](/guide/di/defining-dependency-providers).

adev-ja/src/content/guide/di/creating-and-using-services.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Creating and using services
1+
# サービスの作成と使用
22

3-
Services are reusable pieces of code that can be shared across your Angular application. They typically handle data fetching, business logic, or other functionality that multiple components need to access.
3+
サービスは、Angularアプリケーション全体で共有できる再利用可能なコードです。サービスは通常、複数のコンポーネントがアクセスする必要のあるデータ取得、ビジネスロジック、またはその他の機能を扱います。
44

5-
## Creating a service
5+
## サービスの作成 {#creating-a-service}
66

7-
You can create a service with the [Angular CLI](tools/cli) with the following command:
7+
次のコマンドで[Angular CLI](tools/cli)を使用してサービスを作成できます。
88

99
```bash
1010
ng generate service CUSTOM_NAME
1111
```
1212

13-
This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory.
13+
これにより、`src`ディレクトリに専用の`CUSTOM_NAME.ts`ファイルが作成されます。
1414

15-
You can also manually create a service by adding the `@Injectable()` decorator to a TypeScript class. This tells Angular that the service can be injected as a dependency.
15+
TypeScriptクラスに`@Injectable()`デコレーターを追加して、手動でサービスを作成できます。これにより、そのサービスが依存性として注入可能であることがAngularに伝播します。
1616

17-
Here is an example of a service that allows users to add and request data:
17+
以下は、ユーザーがデータを追加したり要求したりできるサービスの例です。
1818

1919
```ts
2020
// 📄 src/app/basic-data-store.ts
@@ -34,21 +34,21 @@ export class BasicDataStore {
3434
}
3535
```
3636

37-
## How services become available
37+
## サービスが利用可能になる仕組み {#how-services-become-available}
3838

39-
When you use `@Injectable({ providedIn: 'root' })` in your service, Angular:
39+
サービスで`@Injectable({ providedIn: 'root' })`を使用すると、Angularは次のことを行います:
4040

41-
- **Creates a single instance** (singleton) for your entire application
42-
- **Makes it available everywhere** without any additional configuration
43-
- **Enables tree-shaking** so the service is only included in your JavaScript bundle if it's actually used
41+
- **アプリケーション全体で単一のインスタンス** (シングルトン) を作成します
42+
- **追加の設定なしでどこでも利用可能**にします
43+
- **ツリーシェイキングを有効にし**、サービスが実際に使用される場合にのみJavaScriptバンドルに含まれるようにします
4444

45-
This is the recommended approach for most services.
45+
これは、ほとんどのサービスで推奨されるアプローチです。
4646

47-
## Injecting a service
47+
## サービスを注入する
4848

49-
Once you've created a service with `providedIn: 'root'`, you can inject it anywhere in your application using the `inject()` function from `@angular/core`.
49+
`providedIn: 'root'`でサービスを作成すると、`@angular/core``inject()`関数を使ってアプリケーションのどこにでも注入できます。
5050

51-
### Injecting into a component
51+
### コンポーネントへの注入 {#injecting-into-a-component}
5252

5353
```angular-ts
5454
import { Component, inject } from '@angular/core';
@@ -70,7 +70,7 @@ export class ExampleComponent {
7070
}
7171
```
7272

73-
### Injecting into another service
73+
### 別のサービスへの注入 {#injecting-into-another-service}
7474

7575
```ts
7676
import { inject, Injectable } from '@angular/core';
@@ -93,13 +93,13 @@ export class BasicDataStore {
9393
}
9494
```
9595

96-
## Next steps
96+
## 次のステップ {#next-steps}
9797

98-
While `providedIn: 'root'` covers most use cases, Angular offers additional ways to provide services for specialized scenarios:
98+
`providedIn: 'root'`はほとんどのユースケースをカバーしていますが、Angularは特殊なシナリオのためにサービスを提供するための追加の方法を提供しています:
9999

100-
- **Component-specific instances** - When components need their own isolated service instances
101-
- **Manual configuration** - For services that require runtime configuration
102-
- **Factory providers** - For dynamic service creation based on runtime conditions
103-
- **Value providers** - For providing configuration objects or constants
100+
- **コンポーネント固有のインスタンス** - コンポーネントが独自の独立したサービスインスタンスを必要とする場合
101+
- **手動設定** - 実行時の設定が必要なサービス向け
102+
- **ファクトリープロバイダー** - 実行時の条件に基づいて動的にサービスを作成するため
103+
- **値プロバイダー** - 設定オブジェクトや定数を提供するため
104104

105-
You can learn more about these advanced patterns in the next guide: [defining dependency providers](/guide/di/defining-dependency-providers).
105+
これらの高度なパターンについての詳細は、次のガイドで学ぶことができます: [依存性プロバイダーの定義](/guide/di/defining-dependency-providers).

0 commit comments

Comments
 (0)