使用服务在Angular 2应用程序中存储(和共享)初始值的最佳实践是什么?我有一个服务,从服务器加载大量数据作为资源,配置和其他数组和对象.我不希望每次加载组件或路由到视图时都加载此数据,我只想在应用程序启动时使用这些对象和已加载的数组,并根据需要重新加载.问题是存储此值的正确位置以及如何在使用该服务的组件之间共享?谢谢.
I have a shared service (starting from this suggestion) that cache and returns some data after first http request:
export class SharedService {
constructor(private http:Http) {
}
getData() {
if (this.cachedData) {
return Observable.of(this.cachedData);
} else {
return this.http.get(...)
.map(res => res.json())
.do((data) => {
this.cachedData = data;
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
My problem is that I have some directives and components inside the same template that are all initialized at the same time and each one call simultaneously (inside …
有人能告诉我在Angular 2 Typescript组件中使用完整的sprintf库(零填充和标准sprintf中通常存在的所有其他功能)所需的所有步骤吗?谢谢.
我有一项服务,每 3 秒从服务器获取一次数据。有时服务器很慢,许多响应在 5 或 6 秒后出现。发生这种情况时,我的服务开始取消每个请求,而不是等待挂起的请求。我怎样才能防止这种情况?
public getCallDiagnostic():Observable<IRespWidgets>{
let body = JSON.stringify({manager:"CallDiagnosticServiceManager",
action:"GetCallDiagnostic",
WEB_USER_TOKEN:this._authenticationService.getUserToken()});
let options = new RequestOptions({headers: new Headers({'Content-Type': 'application/json'})});
return Observable.timer(0,3000)
.switchMap(()=>this._http.post(this._apiUrl,body,options))
.map(res => <IRespWidgets>res.json().data)
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)