如何在 angular 4 服务中缓存一些数据? [英] How can I cache some data in angular 4 service?

查看:23
本文介绍了如何在 angular 4 服务中缓存一些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,我调用它从我的休息服务中提取一些数据.我在我的服务中设置了一个对象,其中包含来自 rest 调用的结果.问题是当我调用 get 函数返回对象时,我总是未定义.我目前在应用程序根 onInit 函数上调用了我的服务.

I have a service that I call to pull some data from my rest service. I have an object that I set inside my service with the result from the rest call. The problem is that when I call the get function to return the object I always get undefined. I currently have the call to my service on the application root onInit function.

关于如何加载这些数据并存储在对象上的任何帮助,以便下次我需要访问它时,我不需要远程调用我的休息服务?

Any help on how I could load this data and store on the object so next time I need to access it I do not need to make a remote call to my rest service?

这是我的服务

    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.下次使用现有数据时,它会将您的数据存储在辅助服务中.

you can use Observable Of in your function. it will store your data in side service next time it will use exising data.

示例代码

 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);
        }
    }

这篇关于如何在 angular 4 服务中缓存一些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆