2016-07-29 10 views
1

コンポーネント間でサービスを共有することについての読書や、アプリケーションコンポーネントを使用する基本的な考え方は、基本的にプロバイダとしてのサービスのシングルトンを作成することです。Angular2共有HTTPサービス

コンポーネントがネストされているコンポーネントをロードし、ネストされたコンポーネントはすべてこの共有サービスを使用します。将来のイベントがページ上でトリガされ、すべてのネストされたコンポーネントのテンプレート要素を更新および更新するためにHTTPサービスが必要になります。どのようにしてこのアップデートを「強制」するのですか?

また、「root」コンポーネントが読み込まれるたびにHTTPサービスが実行されるように、アプリケーションコンポーネントでサービスを共有していたためですか?

正しく理解していませんか?私はむしろディレクティブを使うつもりですか?

+0

何も強制する必要はありません。 Angular2の変更検出で変更が検出されると、HTMLが更新されます。 HTTPリクエストは、コンポーネントがそのコンポーネントにサブスクライブするときに行われます。サービス・コンストラクターから加入した場合、サービスは最初にどこかに注入されたときに要求されます(これはインスタンスが作成された時です)。 –

+0

私はちょっと考えていますが、サービスが更新されると、サブスクライブされたコンポーネントは更新されます(指示は正しいのですか?)しかし、私は更新が必要な複数のネストされたコンポーネントを持つダッシュボードページとコンポーネントを持っていると言う事実を理解しようとしています。サービスが更新されると、実際にどの時点でサービス更新が呼び出されますか?ネストされたコンポーネントがロードされた後のダッシュボードコンポーネント – Wancieho

+0

サービスを受け取るコンポーネント上のngOnInitインタフェースを使用して初期状態を初期化し、ngOnInitライフサイクルフックメソッド内で初期化ロジックを実行します。その後のサービスの更新は、いずれかのコンポーネントからいつでも呼び出すことができます。 https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html –

答えて

1

更新:私はこの週末に一緒に何かを入れる時間がありませんでしたが、事がまだ明確でない場合は、a simplified example to show how service injection works in Angular 2を作った。

AppComponentはAppServiceを@Componentデコレータのプロバイダとしてリストします。つまり、サービスのシングルトンがこのコンポーネントレベルで注入されます。 ChildComponentでは、サービスがAppComponentに注入された同じインスタンスを使用するため、サービスをプロバイダとしてリストする必要はありません。 AppServiceモジュールをインポートしてコンストラクタにサービスを注入するだけです。

逆に、IsolatedComponentはAppServiceの別のインスタンスを使用しているため、@Componentデコレータのproviders配列を介して新しいシングルトンインスタンスを挿入します。 IsolatedChildComponentは、IsolatedComponentで使用されているのと同じサービスインスタンスを使用します.ChildComponentと同様に、AppServiceモジュールをインポートしてそのコンストラクタ定義にインスタンスを挿入するだけです。

コンポーネントが初期化されるたびに、各コンポーネントが共有バインディングプロパティ、メッセージをどのように更新し、子コンポーネントがこれらの更新を自動的に取得するかに注目してください。同じロジックをAPI呼び出しを行うサービスに適用できます。ここ

は、サービスおよびコンポーネントのコードである:

app.service.ts

import { Injectable } from '@angular/core'; 

@Injectable() 
export class AppService { 
    messages: string[] = []; 

    updateMessages(msg: string) { 
    this.messages.push(msg); 
    } 
} 

app.component.ts

import { Component, OnInit } from '@angular/core'; 
import { AppService } from './app.service'; 
import { ChildComponent } from './child.component'; 
import { IsolatedComponent } from './isolated.component'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1>AppComponent Tree</h1> 
    <p> 
    AppComponent and ChildComponent consume the same instance of AppService 
    </p> 
    <child-component></child-component> 
    <hr /> 
    <isolated-component></isolated-component> 
    `, 
    providers: [AppService], 
    directives: [ChildComponent, IsolatedComponent] 
}) 
export class AppComponent implements OnInit { 
    messages: string[]; 

    constructor(private appService: AppService) { 
    this.messages = appService.messages; 
    } 

    ngOnInit() { 
    this.addMessage(`AppComponent Initialized`); 
    } 

    private addMessage(msg: string) { 
    this.appService.updateMessages(msg); 
    } 
} 

child.component .ts

import { Component, OnInit } from '@angular/core'; 
import { AppService } from './app.service'; 

@Component({ 
    selector: 'child-component', 
    template: ` 
    <div *ngFor="let message of messages">{{message}}</div> 
    ` 
}) 
export class ChildComponent implements OnInit { 
    messages: string[]; 

    constructor(private appService: AppService) { 
    this.messages = appService.messages; 
    } 

    ngOnInit() { 
    this.addMessage(`ChildComponent Initialized`); 
    } 

    private addMessage(msg: string) { 
    this.appService.updateMessages(msg); 
    } 
} 

isolated.component.ts

import { Component, OnInit } from '@angular/core'; 
import { AppService } from './app.service'; 
import { IsolatedChildComponent } from './isolated-child.component'; 

@Component({ 
    selector: 'isolated-component', 
    template: ` 
    <h1>Isolated Component Tree</h1> 
    <p> 
    IsolatedComponent and IsolatedChildComponent consume an 
    instance of AppService separate from the AppComponent tree 
    </p> 
    <isolated-child></isolated-child> 
    `, 
    providers: [AppService], 
    directives: [IsolatedChildComponent] 
}) 
export class IsolatedComponent implements OnInit { 
    messages: string[]; 

    constructor(private appService: AppService) { 
    this.messages = appService.messages; 
    } 

    ngOnInit() { 
    this.addMessage(`IsolatedComponent initialized`); 
    } 

    private addMessage(msg: string) { 
    this.appService.updateMessages(msg); 
    } 
} 

孤立child.component.ts

import { Component, OnInit } from '@angular/core'; 
import { AppService } from './app.service'; 

@Component({ 
    selector: 'isolated-child', 
    template: ` 
    <div *ngFor="let message of messages">{{message}}</div> 
    ` 
}) 
export class IsolatedChildComponent implements OnInit { 
    messages: string[]; 

    constructor(private appService: AppService) { 
    this.messages = appService.messages; 
    } 

    ngOnInit() { 
    this.addMessage(`IsolatedChildComponent initialized`); 
    } 

    private addMessage(msg: string) { 
    this.appService.updateMessages(msg); 
    } 
} 

Hierarchical Injectorsマニュアルを参照してください。

+0

を参照してください。同じサービスに依存する複数のコンポーネントを持つページがある場合、すべてのコンポーネントと "ページコンポーネントの「ボトム」がサービスを呼び出すので、各コンポーネントが更新されます。多分私の質問は完全にはっきりしていませんか? – Wancieho

+0

私はあなたが得ているものを手に入れます。あなたが行うことは、最初の関連する親コンポーネントにプロバイダとしてサービスを注入し、その後の子コンポーネントでサービスをコンストラクタに注入することです。ツリー内のすべてのコンポーネントはサービスの同じインスタンスを参照し、サービスの変更によって影響を受けるバインディングはコンポーネントに自動的に反映されます。 –

+0

ここで、コンポーネントが同じコンポーネントツリーになく、それらがすべて同じサービスで編成されるようにするには、AppComponentにインジェクトすることで、これを行うことができます.AppComponentは、Angularアプリ。 –

関連する問題