How to convert observable data to a number in angular6

元气小坏坏 提交于 2019-12-13 04:25:39

问题


I know this question may have been answered here but it does not address my problem Angular - Convert a Observable<number> to a number

Here, Am trying to pass data as number to my service in angular 6 in order to perform data pagination. If I implement something like the line of code, data will get posted to server back end

getAll(row: string, rowperpage: string): Observable<any> {

Since am working with number that can help me to perform pagination, If implement

 getAll(row: number, rowperpage: number): Observable<any> {

it will show the error below

Error in src/app/service/crud.service.ts(47,52): error TS2345: Argument of type'(row: number, rowperpage: number)' is not assignable to parameter of type'(Headers?: HttpHeaders| {header: string |string[];) observe?: "body"; params?

Please How can i convert an Observable data to a number to pass it to my service in Angular 6

Here is my service.ts

//crud_s: Crud[];

constructor(private http: HttpClient) { }

  getAll(row: number, rowperpage: number): Observable<any> {

    return this.http.get(`${this.baseUrl}/list1`, {row: row, rowperpage: rowperpage}).pipe(
      map((res) => {
        this.crud_s = res['data'];
        return this.crud_s;
    }),
    catchError(this.handleError));
  }

here is my component.ts

row = 0;
rowperpage = 4;

  getCars(): void {

    this.crudService.getAll(this.row, this.rowperpage).subscribe(
      res => {
  //this.row+=this.rowperpage;
//more code coming

        this.crud_s = res;
      },
      (err) => {
        this.error = err;
      }
    );
  }
|improve this question

回答1:


Error you are quoting is not coming from the line you say it is but it comes from here:

 return this.http.get(`${this.baseUrl}/list1`, {row: row, rowperpage: rowperpage})

second argument is request specification, not query data. To add query data you have to do it like this:

 return this.http.get(`${this.baseUrl}/list1`, {params:{row: row, rowperpage: rowperpage}})

or even use HttpParams for this - but still it will be added like above.



来源:https://stackoverflow.com/questions/52979309/how-to-convert-observable-data-to-a-number-in-angular6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!