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
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
+
exportclassBasicDataStore {
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';
Copy file name to clipboardExpand all lines: adev-ja/src/content/guide/di/creating-and-using-services.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
# Creating and using services
1
+
# サービスの作成と使用
2
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.
You can create a service with the [Angular CLI](tools/cli) with the following command:
7
+
次のコマンドで[Angular CLI](tools/cli)を使用してサービスを作成できます。
8
8
9
9
```bash
10
10
ng generate service CUSTOM_NAME
11
11
```
12
12
13
-
This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory.
13
+
これにより、`src`ディレクトリに専用の`CUSTOM_NAME.ts`ファイルが作成されます。
14
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.
This is the recommended approach for most services.
45
+
これは、ほとんどのサービスで推奨されるアプローチです。
46
46
47
-
## Injecting a service
47
+
## サービスを注入する
48
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`.
0 commit comments