I have the following components in an Angular site:
product-api.service.ts:
export class ProductApiService {
constructor(private api: ApiHandlerService) { }
getProductsFromAPI() : Observable<Product[]> {
return this.api.get<Product[]>("/api/products");
}
}
product.service.ts:
export class ProductService {
constructor(private productApiSvc: ProductApiService) {}
getProducts() : Product[] {
this.productSvc.getProductsFromAPI().subscribe(
result => {
// return list here
}
)
}
}
product-list.component.ts
products: Product[];
constructor(private productSvc: ProductService) { }
ngOnInit(): void {
this.products = this.productSvc.getProducts();
}
My component class is using product.service
class's getProducts()
method to get list of products. The problem is getProducts
returns Observable<Product[]>
. How can I convert this to return Product[]
? Is it better to remove product.service.ts
wrapper and call getProductsFromAPI
directly from list.component.ts
class? If so, what are the benefits of it? Thanks!