0

can some one suggest me how to avoid multiple api calls while using same component multiple places in same container

class ChildComponent{
 // some api calls and actions (api call is made in service)
}

parent.html
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
4
  • Can you be more specific about your problem? Please share some information about the service class implementation, relationship between parent and child components and if the data you are fetching can be cached or not. Commented Jul 9, 2021 at 12:01
  • 2
    you can call the apis in the "parent", create an object and pass as input to your "childs". You can also use rxjs operator share, take a look itnext.io/… Commented Jul 9, 2021 at 12:28
  • @Shravan we don't have much code in service just making and http call and returning observable. Commented Jul 10, 2021 at 3:21
  • @Eliseo thanks i appreciate for your time, this seems to fine i have implemented the same way currently i was checking weather we have any alternative than this Commented Jul 10, 2021 at 3:23

1 Answer 1

1

I had similar problem.

What I did is to create a flag in that component which initially is set to false. Now where you call that API you check if that flag is false and only then you call the API and inside the call you set that flag to true. Next instance of the same component will not make that API call as the flag will be true

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.