需要在 Angular 4 中从服务到组件获取大量数据

Need to get large chunk of data from service to component in Angular 4

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

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

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

npm i indexeddb-angular

在这种情况下,RxJs partition 运算符可能会有所帮助。假设 'large object' 是带有键的嵌套对象,您可以按您想要的顺序对 return 实施一些逻辑。

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