"componentWillLoad" will not always get triggered when page is loaded

Hello guys

First off, I wanted to let you know that I’ve been already trying to figure this out on my own for some time without any success, that’s why I finally decided to look for help here. It’s also important to clarify that I’m quite new to this, I started just this year with web development, so I’m still ignorant on many things and I’m really sorry I’m my terminology is a little off.

That said, I have the following problem: I need some code to be executed every time a page is loaded, and I thought “componentWillLoad” was the right choice for this. However, I can’t really comprehend its behavior. Why does it sometime get triggered and some times it doesnt?

Here’s a video showing what I mean:


I have a console.log inside of a “componentWillLoad” function in every page, and the following happens:

  • The first time I go into “/”, it gets triggered. No problem.
  • Then, when I go from “/” to “/dashboard” it gets triggered. No problem
  • Then, from “/dashboard” to “/project” it gets triggered. No problem.
  • Then, when I go back from “/project” to “/dashboard” it doesn’t get triggered. However, if I go again from “/dashboard” to “/project” it does get triggered every time. Why does this happen?

To switch between pages I use this in every case:

this.navCtrl.push("/url", "forward")

Please let me know if you need more information. In case you need to take a look at the code, it’s here (I’m pretty sure the code is a mess in terms of best practices, so sorry about that). The important parts are in the “component” foldes, specifically in the components “app-login”, “app-project” and “app-dashboard”.

Thanks in advance guys!

Sorry, I use Angular as a framework, so somebody else is going to have to handle the details here, but generally speaking,

My advice is to design your app in such a way that the answer to this question is totally irrelevant, and this is not a mere academic concern. Internal framework implementation details like this are subject to change, and often not in predictable or documented ways. It’s entirely possible that you will see different results running in different environments.

Thanks for your quick answer rapropos. I think you’re right, in this case I think that maybe I’m just not using the right approach (?). Like I said, I need some code to be executed every time the page is loaded (a request to the database). After reading the documentation I thought “componentWillLoad” was the right place to do so, but after trying the whole day and getting so unpredictable results, I’m not so sure anymore.