In my Angular (10) application I use data that are specific for each request. I use part of the URL domain name to make some business decisions.
In my application (just early development) I parse the URL, save this data in the public variable of the app (inside of AppComponent class) and use it when needed.
At some point, I decided to move some logic from the app.component.ts to child components. And because this data can be used by a few components, rather than parsing it many time inside of each component I'm thinking about parsing it once in the "AppComponent" and passing it down as a parameter.
Though, computational expenses on passing parameter might be higher than parsing it again so:
Question #1: is it reasonable to pass data to a component that the component might obtain on its own?
When data are passed to the component, I use them in 2 cases: 1) on the HTML view itself, 2) inside the onNgInit().
Data on the HTML view are rendered completely perfect, but inside of onNgInit, my property is not initialized yet. I suspect this is due to the components' lifecycle (which I don't know well) and something is not ready yet so I don't have access to the data.
As a workaround, I could move my logic in the component from onNgInit to ngAfterContentInit on maybe to ngAfterViewInit. But what if my component would need to pass data further to another sub-component...?
Question #2: How to pass data from one component to another so if it is initialized inside onNgInit of the parent then it is also available inside of onNgInit of the child.
Thanks!
https://stackoverflow.com/questions/67052168/should-component-extract-data-from-storage-or-receive-from-the-parent April 12, 2021 at 11:06AM
没有评论:
发表评论