【问题标题】:Need to get large chunk of data from service to component in Angular 4需要在 Angular 4 中从服务到组件获取大量数据
【发布时间】:2019-01-23 05:30:00
【问题描述】:

我需要将一个非常大的对象从服务传递给我的组件。由于对象非常大,我打算将对象分成块。 如何每隔一段时间发送服务“getData()”请求以逐块获取数据? 基本上我不希望数据一次来自服务,而是分块。 例如,如果数据是这样的 - {'x':'1','y':2,'z':3},那么数据首先是 x,然后是 y,依此类推。 提前感谢您的提醒。

【问题讨论】:

  • 你试过从服务器端使用分页吗?所以服务器会根据你的需要和调用页面向你发送数据。
  • 我有一个要求实际上不使用分页。

标签: html angular angular2-services


【解决方案1】:

在这种情况下,RxJs partition 运算符可能会有所帮助。假设“大对象”是带有键的嵌套对象,您可以实现一些逻辑以按您想要的顺序返回。

const obj = {fist: {name: 'joe', age: 30}, second: {name: 'doe', age: 40}};
//Turn it into iterable
const array = Object.entries(obj);
// make an observable
const source = from(array);
// Logic for how to split, depends on your obj structure
const [under, over] = source.pipe(partition(val => val[1].age < 40));
// result
under.subscribe(a => console.log("Under 30: " + a));
over.subscribe(a => console.log("Over 30: " + a));

【讨论】:

    【解决方案2】:

    选项 1: 当您获得数据时,然后转储到本地存储或 cookie 中,然后在需要时读取。

    选项 2: 如果你有这么多的大数据,那么用角度为 indexDB 做一些 RND。它会帮助你。 :) –

    npm i indexeddb-angular

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2018-02-14
      • 2017-11-04
      • 2020-09-08
      • 2017-12-24
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多