2016-06-21 67 views
1

THIS開始,我想在另一個全局服務(SystemService)中使用許多DataService實例來存儲和提供許多「可緩存」數據。 我已經導入了DataService文件並在我的全局服務的構造函數中聲明瞭DataService類,但是在運行時我收到錯誤「EXCEPTION:沒有DataService!(AppComponent - > SystemService - > DataService)提供程序」 請問任何人都可以解釋如何在另一個全球服務中使用該服務? 非常感謝。Angular2,在另一個服務中使用服務

編輯,只是示例代碼

--------- 
// inside bootstrap only the single instance of SystemService 
.... 
bootstrap(AppComponent, [ROUTER_PROVIDERS,HTTP_PROVIDERS,SystemService,provide(LocationStrategy, {useClass: HashLocationStrategy})]). 
then((appRef: ComponentRef) => { 
    appInjector(appRef.injector); 
}); 

----------- 
// chacheable and observable generic Data service, I want to use this class in multiple instances one for each cachable and observable data 

import {Injectable} from        'angular2/core'; 
import {Http, Response,Headers, RequestOptions} from 'angular2/http'; 
import {Observable} from        'rxjs/Observable'; 
import {Observer} from         'rxjs/Observer';  
import             'rxjs/Rx'; 
@Injectable(
export class CacheableData<T> { 
    private _apiUrl = 'api/apiweb.php'; 
    private _params:Object; 

    public data: Observable<T>; 
    private _dataObserver: Observer<T>; 
    private _data:T; 

    constructor (private _http: Http){ 
     this.data = new Observable(observer => this._dataObserver = observer).startWith(this._data).share(); 
    } 
    public setParams(_params:Object){ 
     this._params=_params; 
    } 
    public getData(refresh:boolean=false) {  
     if (this._data && !refresh) { 
      console.log('CacheableData - getData -> Cache',this._data); 
      this._dataObserver.next(this._data);  
     } else {    
      console.log('CacheableData - getData -> HTTP...'); 

      this._http.post(this._apiUrl, 
        JSON.stringify(this._params), 
        new RequestOptions({ headers: new Headers({'Content-Type': 'application/json'})}))   
       .map(res=><T>res.json().data) 
       .do((data) => { })  
       .subscribe(res => { 
        // Update cached data 
        this._data = res;     

        console.log('CacheableData - getData -> HTTP',this._data); 
        this._dataObserver.next(this._data); 
       }, error => console.log('Could not load data.'))        

     } 
    } 
} 
--------- 
// the global service used to cache and retrieve certain type of datas from the server 
@Injectable() 
export class SystemService{ 
    constructor (public cacheableData1: CacheableData<IData1>, 
      public cacheableData2: CacheableData<IData2>) { 

     .... 
     this.cacheableData1.setParams({ 
      manager:"ApiManager", 
      action:"GetData1", 
      WEB_USER_TOKEN:this.user.Token 
     }); 

     this.cacheableData2.setParams({ 
      manager:"ApiManager", 
      action:"GetData2", 
      WEB_USER_TOKEN:this.user.Token 
     }); 
     ..... 

    } 
} 


--------- 
// all views can read and observe chached or fresh data using the systemservice 
@Component({ 
    selector: 'home', 
    templateUrl: 'app/components/views/home/home.component.html' 
}) 
export class HomeComponent implements OnInit{ 
    public data1:IData1; 
    public data2:IData2; 

    constructor(private _router: Router,       
      private _systemService: SystemService) { 
    } 
    ngOnInit(){ 

     // data1 observable   
     this._systemService.cacheableData1.subscribe(data => this.data1=data); 
     this._systemService.cacheableData1.getData(); 

     // data2 observable   
     this._systemService.cacheableData2.subscribe(data => this.data2=data); 
     this._systemService.cacheableData2.getData(true); 

    } 
} 

當然我錯過了什麼,但這個概念是,我需要SystemService的全局實例使用CacheableData服務的多個instalces,以提供可觀察和緩存數據所有的觀點和網頁...或其他服務,爲什麼不。 我不能添加CacheableData引導,因爲我需要多個實例,但如果我不這樣做,我收到錯誤消息,提供程序丟失... 對不起,我的項目是相當複雜的....

+0

您可以添加演示您嘗試過的代碼嗎? –

回答

0

要使用的每個服務(取決於範圍)都必須添加到提供程序數組中。

無論是在bootstrap電話:

bootstrap(AppComponent, [ 
    ..., 
    GlobalService, 
    DataService 
]); 

或者在你的根組件:

@Component({ 
    ..., 
    providers: [..., GlobalService, DataService] 
}) 
export class AppComponent { ... } 
+0

感謝您的回答, 問題是,在SystemService中,我需要多個不同的DataService實例來替換多個不同的數據。 將DataService添加到引導僅創建一個實例,如果我錯誤,請糾正我。 –

+0

如何有多個DataServices?你會在哪裏實例化這些? – rinukkusu

1

你檢查分發引用例如服務A使用服務B,服務B使用服務A?我最近遇到過這類問題,錯誤信息與您的相同。

相關問題