简体   繁体   中英

how use shareReplay(1) in angular?

picture of error I have Error when use shareReplay(1) in angular13 my code is

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { shareReplay } from 'rxjs-compat/operator/shareReplay';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
interface mydatamodel{
  something:any;
}

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

  data$;
  cachedUsers$: Observable<any>;
  constructor(private http: HttpClient) { }

  private apiURL = environment.apiURL


  getUsers(filename:string,database:string,procedure:string,ParamList:string): Observable<any>{
    let finalurl=this.apiURL;
    let params = new HttpParams();
    params = params.append('filename',filename);
    params = params.append('database',database);
    params = params.append('procedure',procedure);
    params = params.append('ParamList',ParamList);
    if (!this.cachedUsers$) {
      this.cachedUsers$ = this.http.get(finalurl, {params: params}).pipe(
        map((response: any) => response.data),
        shareReplay(1)
      );
    }
    return this.cachedUsers$;
  }
}

this Error is showing

No overload matches this call. Overload 1 of 2, '(this: Observable, config: ShareReplayConfig): Observable', gave the following error. The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable'. Overload 2 of 2, '(this: Observable, bufferSize?: number | undefined, windowTime?: number | undefined, scheduler?: SchedulerLike | undefined): Observable', gave the following error. The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable'.ts(2769)

Problem is likely coming as well from the import, it should be : import { shareReplay } from 'rxjs/operators';

Here is an example working: https://stackblitz.com/edit/angular-ivy-rzbd1v?file=src/app/app.service.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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