TL;DR: I have a child component that is excluded by *ngIf, but when the parent component loads, the "excluded" component gets constructed/initialized (but not rendered).
Overview:
- Let's refer to the parent component as ArticleComponent
- Let's refer to the child components as BlogArticle and DocumentationArticle
- ArticleComponent calls a back-end API which returns a json doc that either has a
blogordocumentationproperty set (but never both, one is alwaysundefined) *ngIfin ArticleComponent renders either the BlogArticle or DocumentationArticle template
Behavior:
If the current child is the DocumentationArticle, and I navigate to a blog url (via a [RouterLInk]), through logging I observe that the DocumentationArticle's constructor and ngOnInit methods are being called, even though the template is never rendered.
This describes in more detail how the code is structured:
- Use navigates to a url in the app
- Routing matches ArticleComponent
- ArticleComponent subscribes to
Router.urland determines what back-end resource needs to be queried, then pushes that resource URI into an NgRx store - NgRx effect listens for that action, calls the back-end API, and pushes the loaded document to the store
- ArticleComponent is subscribed to the NgRx store, and when a new document is pushed,
asyncpipe renders one of the two child components that are each guarded by*ngIf
This is only concerning because ngOnInit in the DocumentationComponent calls backend services for other data, which are needlessly being invoked (aside from when they should).
What should I be looking for as I dig into my code? Ultimately I'd like to learn what bad behaviors I'm introducing that do not play well with the angular's cycles short of being told exactly what the problem is 🙂