从THIS非常棒的响应开始,我想在另一个存储和提供许多“可缓存”数据的全局服务(SystemService)中使用许多DataService实例。 我已导入DataService文件并在我的全局服务的构造函数中声明了DataService类,但在运行时我收到错误“EXCEPTION:DataService没有提供者!(AppComponent - > SystemService - > DataService)” 请问,任何人都可以解释如何在另一个全球服务中使用该服务吗? 非常感谢。
编辑,代码只是例如
---------
// 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服务的多个安装,以便为所有视图和页面提供可观察和可缓存的数据......或者为其他服务,为什么不呢? 。 我无法将CacheableData添加到bootstrab,因为我需要多个实例,但如果我不需要,我会收到提供程序丢失的错误消息... 请原谅我的代码不完整,但我的项目非常复杂....
答案 0 :(得分:1)
您是否检查了流通参考,例如服务A使用服务B,服务B使用服务A?我最近遇到过这种问题,错误信息和你的一样。
答案 1 :(得分:0)
您要使用的每个服务(取决于范围)都必须添加到提供程序数组中。
在bootstrap
来电:
bootstrap(AppComponent, [
...,
GlobalService,
DataService
]);
或者在根组件中:
@Component({
...,
providers: [..., GlobalService, DataService]
})
export class AppComponent { ... }