我需要按角度从一种方法调用数据到另​​一种方法?

时间:2018-10-25 07:31:48

标签: angular typescript angular6

我有一个服务文件,其中我调用了两个具有两个api的方法。我需要在deleteData中调用getData方法。有人可以帮我吗

.service.file
getData(): Promise<PagedResult<Sites>> {
    
    const url = `${environment.DATA_API_URL}/sites/GetSites`;
    console.log(url);
    return this.httpClient.get<PagedResult<Sites>>(url).toPromise().then((sites: PagedResult<Sites>) => {
      alert();
      console.log("sites: ",sites);
      });
  }

  deleteData(siteId: String): Observable<{}>{
      const url = `${environment.HELLO_API}/Data?siteId=`+ siteId;
      return this.httpClient.post(url, this.getSites)
      .pipe(
        catchError(this.handleError('deletSites'))
      );
  }
  
  
  
  
  
  
  .ts file
  
  async ngOnInit(){
     
      
      this.sites = await this.dataService.getData();
      console.log(this.data)
       
    }
  
  
  
  
  
  

1 个答案:

答案 0 :(得分:1)

Rannes我建议您使用Observable(将Promise分开)。然后使用Rxjs进行连接,转换...可观察对象。因此,请尝试不要“订阅”服务

因此,您的服务可以像

$result = $repo->findBy(['id_user_one' => 1, 'id_user_two' => 1]);

好吧,现在您可以在组件中“播放”可观察对象了

//return an observable
getData(): Observable<PagedResult<Sites>> {

    const url = `${environment.DATA_API_URL}/sites/GetSites`;
    //See that is a "simple" get
    return this.httpClient.get<PagedResult<Sites>>(url);
  }

  deleteData(siteId: String): Observable<{}>{
      const url = `${environment.HELLO_API}/Data?siteId=`+ siteId;
      return this.httpClient.post(url, this.getSites)
      .pipe(
        catchError(this.handleError('deletSites'))
      );
  }