Search code examples
javascriptangulartypescriptangular7angular8

Best place to implement the subscribe of an observable in angular


I'm developing an app using angular in the FrontEnd and I'm calling rest services with http client and I want to know where is the best place to implement the subscribe of the httpClient.get() 's observable in the service or in the component.ts?


Solution

  • Suppose you have a service that holds logic for calling an api. in your service file

    Abc(): Observable<any>{ // 'any' to be replaced by type
      return this.httpClient.get(url);
    }
    

    in your component file

        this.Service.Abc().subscribe(
        (res) => {
            console.log(res);
        }
    )