THIS부터 시작하여 나는 많은 "캐시 가능한"데이터를 저장하고 제공하는 다른 글로벌 서비스 (SystemService)에서 많은 DataService 인스턴스를 사용하고 싶습니다. DataService 파일을 가져 와서 내 전역 서비스의 생성자에서 DataService 클래스를 선언했지만 런타임에 "예외 : DataService! (AppComponent -> SystemService -> DataService) 공급자가 없습니다"라는 오류 메시지가 표시됨 제발, 다른 글로벌 서비스 내에서 해당 서비스를 사용하는 방법? 정말 고마워요.Angular2, 다른 서비스 내부에서 서비스 사용
편집, 단지 예를 들어 코드 분명히 내가 뭔가를 그리워하지만, 개념은 내가 대한 관찰 및 캐시 데이터를 제공하기 위해 CacheableData 서비스의 여러 instalces를 사용하여 시스템 서비스의 하나 개의 글로벌 인스턴스를 필요로한다는 것입니다
---------
// 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);
}
}
모든보기 및 페이지 ... 또는 다른 서비스의 경우 왜 그렇지 않습니다. 다중 인스턴스가 필요하므로 CacheableData를 부트 스트랩에 추가 할 수 없지만 공급자가 누락되었다는 오류 메시지가 표시됩니다 ... 불완전한 코드에 대해 죄송하지만 프로젝트가 복잡합니다 ....
시도한 것을 나타내는 코드를 추가해주십시오. –