2016-11-02 5 views
0

ように私はng2-adminテーマから、次のスピナーを使用しています。アプリケーション全体でどのように利用できるのですか?使用スピナーは、グローバルなサービス

+0

各コンシューマからのインポートには、主に依存関係を呼び出すいくつかの利点があります。これを後で 'import {SpinnerService} from ...'行なしで検索して置き換えることを想像してみてください。 – ssube

+0

しかし、アプリ内のスピナーサービスは、通常、すべてのコンポーネントで同じです。それが変更されると、アプリ全体が更新されます – FacundoGFlores

答えて

1

あなたは

export class BaseView { 

    protected _injector:Injector; 

    protected _spinnerService:SpinnerService; 

    constructor() { 
     let providers = ReflectiveInjector.resolve([SpinnerService]); 
     this._injector = ReflectiveInjector.fromResolvedProviders(providers); 
    } 

    get spinnerService(): SpinnerService { 
     if (this._spinnerService == null) { 
      this._spinnerService = this._injector.get(SpinnerService); 
     } 
     return this._spinnerService; 
    } 
} 

のようなゲッターを基本コンポーネントを作成して置くことができ、それを使用します。

this.spinnerService.show() 

ReflectiveInjectorは、角度/コア@の中に発見することができ

ドキュメント:https://angular.io/docs/ts/latest/api/core/index/ReflectiveInjector-class.html

+0

appInjectorは何ですか?どこからインポートしますか? – FacundoGFlores

+0

@FacundoGFlores '{Anger/Core 'からのインポート{Injector}; –

+0

@FacundoGFlores私は答えを更新しました。これはあなたの情報のためだけにサービスロケータパターンです。 –

関連する問題