相关疑难解决方法(0)

将分页请求转换为带有RxJ的Observable流

我有一个服务,它返回页面中的数据.对一个页面的响应包含有关如何查询下一页的详细信息.

我的方法是返回响应数据,然后如果有更多页面可用,则立即将延迟调用连接到相同的可观察序列.

function getPageFromServer(index) {
  // return dummy data for testcase
  return {nextpage:index+1, data:[1,2,3]};
}

function getPagedItems(index) {
  return Observable.return(getPageFromServer(index))
    .flatMap(function(response) {
      if (response.nextpage !== null) {
        return Observable.fromArray(response.data)
          .concat(Observable.defer(function() {return getPagedItems(response.nextpage);}));
      }

      return Observable.fromArray(response.data);
    });
}

getPagedItems(0).subscribe(
  function(item) {
    console.log(new Date(), item);
  },
  function(error) {
    console.log(error);
  }
)
Run Code Online (Sandbox Code Playgroud)

这一定是错误的方法,因为在2秒内你得到:

      throw e;
            ^
RangeError: Maximum call stack size exceeded
    at CompositeDisposablePrototype.dispose (/Users/me/node_modules/rx/dist/rx.all.js:654:51)
Run Code Online (Sandbox Code Playgroud)

分页的正确方法是什么?

javascript reactive-extensions-js rxjs

9
推荐指数
2
解决办法
3651
查看次数

Angular 中如何从 json 中获取数据分页

我是新手,我有一个 HTTPclient 服务,它从一个 JSON 文件中获取所有数据,有 1000 个寄存器,我如何进行分页,例如,显示前 10 个并对其进行分页

这是我的服务

@Injectable({
  providedIn: 'root'
})
export class ProductsService {

  constructor(private http:HttpClient ) { 
  }

  getPorducts(): Observable<Product[]> {

    return this.http.get<Product[]>('assets/data/DATA.json');

  }
}
Run Code Online (Sandbox Code Playgroud)

我只是在我的家庭组件上使用它

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styles: []
})
export class HomeComponent implements OnInit {

  public products: any[] = [];


  constructor(private _Products: ProductsService) { }

  public lastKey: any[] = [];

  ngOnInit() {
    this._Products.getPorducts().subscribe(data => {
      if (data) {
        this.products = data;
      }
    });


  }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到呢?提前致谢

javascript arrays httpclient angular

1
推荐指数
1
解决办法
3450
查看次数