たとえば、私はいくつかの機能を持っていますfooA()
、fooB()
...多くのコンポーネントで使用されています。いつAngularでサービスを作成する必要がありますか?
これらのメソッドをサービスクラスにラップして挿入するか、これらの静的関数を持つクラスをエクスポートする必要がありますか?
サービスでラップ:
...
@Injectable()
export class SampleService {
fooA() {...}
fooB() {...}
}
@Component({
...
providers: [SampleService]
})
export class SampleComponent {
constructor(private sampleService: SampleService) {...}
method() {
this.sampleService.fooA();
}
}
は、単にクラスをエクスポートする:あなたが再利用されているメソッドを持って
export class Utils {
static fooA() {...}
static fooB() {...}
}
@Component({...})
export class SampleComponent {
method() {
Utils.fooA();
}
}
最新のコードスニペットをご覧ください。申し訳ありませんが私は明確に私の質問を記述していない。 – Ran