简体   繁体   English

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

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

I need to pass a very large object to my component from service. 我需要通过服务将非常大的对象传递给组件。 Since the object is very large i am intending to divide the object into chunks. 由于对象非常大,因此我打算将对象分成多个块。 How can i send the service 'getData()' request at intervals to get the data chunk by chunk? 我如何间隔发送服务'getData()'请求以逐块获取数据? Basically i don't want the data to come from service at once, but in chunks. 基本上我不希望数据一次来自服务,而是成块地发送。 For instance if data is like this - {'x':'1','y':2,'z':3}, then data to to come for x first, then y and so on. 例如,如果数据是这样的-{'x':'1','y':2,'z':3},则数据首先出现x,然后是y,依此类推。 Thanks in advance for any heads up. 在此先感谢您的注意。

Option 1: when you got data then dump in local-storage or in cookie then read when you need. 选项1:收到数据后,将其转储到本地存储或Cookie中,然后在需要时读取。

Option 2: If you have so much large data then do some RND for indexDB with angular. 选项2:如果您有这么大的数据,请对带角度的indexDB进行一些RND。 It will help you. 它会帮助你。 :) – :) –

npm i indexeddb-angular npm我indexddb-angular

In this case RxJs partition operator might help. 在这种情况下,RxJs 分区运算符可能会有所帮助。 Assuming 'large object' is nested object with keys you can implement some logic to return in a sequence you want. 假设“大对象”是带有键的嵌套对象,则可以实现一些逻辑以按所需顺序返回。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM