user3528733 user3528733 - 1 year ago 100
AngularJS Question

Angular 2, @Input or OnInit lifecycle hook and hitting server?

I am wondering which solution is better. I have url like : http://localhost:3000/product/3. I have two options to pass Product model to component:

  1. Using @Input and pass product (from another component) without hitting the server

  2. Implement OnInit lifecycle hook and always hitting the server when user invoke this url.

Of course better performance is in first solution but I am not sure if this is proper solution. Any recomendations or another issue which can appear in first solution ? Thanks in advance.

Answer Source

It is very depend on which data you are trying to pull from server. Let's talk about two cases:

  1. If server will host millions of products there is absolutely no reason to get all that products before user will ask for special one. So in this case, onInit request will be more reasonable.
  2. But in case you know that server always fixed small amount of products, let's say - 50 products. I would say in this case you can take them on the init of application and be sure, that user will not have to wait each time he is jumping between products.

In production reality case #1 is more common, because you never know how many products will be pushed by content managers. But take decision according to your business rules.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download