mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-06-11 02:33:10 +08:00
Translated 85 skill sub-reference files to achieve full parity with the English source: - skills/angular-developer/references/ — 35 files (all references) - skills/remotion-video-creation/rules/ — 28 files (all rules) - skills/tinystruct-patterns/references/ — 5 files - skills/openclaw-persona-forge/references/ — 6 files - skills/skill-comply/prompts/ — 3 files - skills/lead-intelligence/agents/ — 4 files - skills/brand-voice/references/ — 1 file - skills/frontend-slides/ — 2 files - hooks/memory-persistence/README.md — 1 file English source parity: 0 missing files (excluding rules/zh/, internal docs, and experimental examples absent from zh-CN)
98 lines
3.3 KiB
Markdown
98 lines
3.3 KiB
Markdown
# サービスの作成と使用
|
|
|
|
Angular のサービスは、複数のコンポーネントや他のサービスがアクセスする必要があるデータフェッチ、ビジネスロジック、または状態管理を扱う再利用可能なコードです。
|
|
|
|
## サービスの作成
|
|
|
|
Angular CLI を使用してサービスを生成できます:
|
|
|
|
```bash
|
|
ng generate service my-data
|
|
```
|
|
|
|
または、TypeScript クラスを手動で作成して `@Injectable()` でデコレートすることもできます。
|
|
|
|
```ts
|
|
import {Injectable} from '@angular/core';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class BasicDataStore {
|
|
private data: string[] = [];
|
|
|
|
addData(item: string): void {
|
|
this.data.push(item);
|
|
}
|
|
|
|
getData(): string[] {
|
|
return [...this.data];
|
|
}
|
|
}
|
|
```
|
|
|
|
### `providedIn: 'root'` オプション
|
|
|
|
ほとんどのサービスには `providedIn: 'root'` の使用が推奨されます。これにより Angular は以下を行います:
|
|
|
|
- アプリケーション全体に対して**シングルインスタンス(シングルトン)を作成**します。
|
|
- `providers` 配列に列挙しなくても**どこからでも自動的に利用可能**にします。
|
|
- **ツリーシェイキングを有効**にし、サービスが実際にどこかで注入されている場合にのみ最終的な JavaScript バンドルに含まれるようにします。
|
|
|
|
## サービスの注入
|
|
|
|
サービスを作成したら、`inject()` 関数を使用してコンポーネント、ディレクティブ、または他のサービスに注入できます。
|
|
|
|
### コンポーネントへの注入
|
|
|
|
```ts
|
|
import {Component, inject} from '@angular/core';
|
|
import {BasicDataStore} from './basic-data-store.service';
|
|
|
|
@Component({
|
|
selector: 'app-example',
|
|
template: `
|
|
<div>
|
|
<p>Data items: {{ dataStore.getData().length }}</p>
|
|
<button (click)="dataStore.addData('New Item')">Add Item</button>
|
|
</div>
|
|
`,
|
|
})
|
|
export class Example {
|
|
// サービスをクラスフィールドとして注入する
|
|
dataStore = inject(BasicDataStore);
|
|
}
|
|
```
|
|
|
|
### 別のサービスへの注入
|
|
|
|
サービスはまったく同じ方法で他のサービスを注入できます。
|
|
|
|
```ts
|
|
import {Injectable, inject} from '@angular/core';
|
|
import {AdvancedDataStore} from './advanced-data-store.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class BasicDataStore {
|
|
// 別のサービスを注入する
|
|
private advancedDataStore = inject(AdvancedDataStore);
|
|
|
|
private data: string[] = [];
|
|
|
|
getData(): string[] {
|
|
// このサービスと注入されたサービスのデータを結合する
|
|
return [...this.data, ...this.advancedDataStore.getData()];
|
|
}
|
|
}
|
|
```
|
|
|
|
## 高度なサービスパターン
|
|
|
|
`providedIn: 'root'` がほとんどのシナリオをカバーしていますが、以下のような場合に他の方法が必要になることがあります:
|
|
|
|
- **コンポーネント固有のインスタンス**:コンポーネントがサービスの独立したインスタンスを必要とする場合、コンポーネントの `@Component({ providers: [MyService] })` 配列で直接提供します。
|
|
- **ファクトリープロバイダー**:動的な作成のため。
|
|
- **値プロバイダー**:設定オブジェクトを注入するため。
|