如何在 angular 4 服务中缓存一些数据?
How can I cache some data in angular 4 service?
我调用了一项服务来从我的休息服务中提取一些数据。我有一个对象,我在我的服务中设置了 rest 调用的结果。问题是,当我将 get 函数调用到 return 对象时,我总是未定义。我目前在应用程序根 onInit 函数上调用我的服务。
关于如何加载此数据并将其存储在对象上以便下次我需要访问它时不需要远程调用我的休息服务的任何帮助?
这是我的服务
import { Injectable } from '@angular/core';
import { Headers, RequestOptions } from '@angular/http';
import { HttpinterceptorService } from './httpinterceptor.service';
import { TopicStatus } from '../interfaces/topicstatus';
import 'rxjs/add/operator/map';
@Injectable()
export class TopicService {
baseUrl: string;
statusObj: TopicStatus[];
constructor(private http: HttpinterceptorService) {
this.baseUrl = '/test/restcall/';
}
getListItems(token: string, topicType: string, topicStatus?: number) {
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers});
let body = 'ooo=' + token + '&type=' + topicType;
if(topicStatus !== undefined) {
body += '&status=' + topicStatus;
}
return this.http.post(this.baseUrl + 'test/restcall', body, options)
.map((res) => res.json());
}
getTopicStatus(token: string) {
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers}),
body = 'ooo=' + token;
this.http.post(this.baseUrl + 'test/restcall', body, options).map((res) => {
this.statusObj = res.json();
});
}
}
感谢您的帮助。
您可以在函数中使用 Observable Of
。它会在下次使用现有数据时将您的数据存储在辅助服务中。
示例代码
getListItems() {
if(this.storeInfo != null)
{
return Observable.of(this.storeInfo);
}
else
{
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers});
let body = 'ooo=' + token + '&type=' + topicType;
if(topicStatus !== undefined) {
body += '&status=' + topicStatus;
}
return this.http.post(this.baseUrl + 'test/restcall', body, options)
.map(res => <Contact[]> res.json())
.do(storeInfo => this.storeInfo = storeInfo)
.catch(this.handleError);
}
}
我调用了一项服务来从我的休息服务中提取一些数据。我有一个对象,我在我的服务中设置了 rest 调用的结果。问题是,当我将 get 函数调用到 return 对象时,我总是未定义。我目前在应用程序根 onInit 函数上调用我的服务。
关于如何加载此数据并将其存储在对象上以便下次我需要访问它时不需要远程调用我的休息服务的任何帮助?
这是我的服务
import { Injectable } from '@angular/core';
import { Headers, RequestOptions } from '@angular/http';
import { HttpinterceptorService } from './httpinterceptor.service';
import { TopicStatus } from '../interfaces/topicstatus';
import 'rxjs/add/operator/map';
@Injectable()
export class TopicService {
baseUrl: string;
statusObj: TopicStatus[];
constructor(private http: HttpinterceptorService) {
this.baseUrl = '/test/restcall/';
}
getListItems(token: string, topicType: string, topicStatus?: number) {
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers});
let body = 'ooo=' + token + '&type=' + topicType;
if(topicStatus !== undefined) {
body += '&status=' + topicStatus;
}
return this.http.post(this.baseUrl + 'test/restcall', body, options)
.map((res) => res.json());
}
getTopicStatus(token: string) {
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers}),
body = 'ooo=' + token;
this.http.post(this.baseUrl + 'test/restcall', body, options).map((res) => {
this.statusObj = res.json();
});
}
}
感谢您的帮助。
您可以在函数中使用 Observable Of
。它会在下次使用现有数据时将您的数据存储在辅助服务中。
示例代码
getListItems() {
if(this.storeInfo != null)
{
return Observable.of(this.storeInfo);
}
else
{
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
options = new RequestOptions({ headers: headers});
let body = 'ooo=' + token + '&type=' + topicType;
if(topicStatus !== undefined) {
body += '&status=' + topicStatus;
}
return this.http.post(this.baseUrl + 'test/restcall', body, options)
.map(res => <Contact[]> res.json())
.do(storeInfo => this.storeInfo = storeInfo)
.catch(this.handleError);
}
}