Difference between events listener codes


#1

I am trying to triger an event from a provider/service to app.component.ts.
Are there differences in behaviour of these two event listener codes?
In my case it is present in app.component.ts file to listen to even trigered from another service.

The 'events' is variabel of type 'Events' imported from 'ionic-angular'.

1. app.component.ts -

ngOnInit(){
    this.events.subscribe('update-menu-title', (userInfo) => { // listener for "update-menu-title" event
        // Usage of 'userInfo'
    })
}

2. app.component.ts -

ngOnInit(){
    this.getUserInfo();
}

getUserInfo(){
    this.events.subscribe('update-menu-title', (userInfo) => { // listener for "update-menu-title" event
        // Usage of 'userInfo'
    })
}

This is how I am trigerring event from another provider/service.

this.events.publish('update-menu-title', this.currentUser); // Update menu title

In my case the 1st way is working for all builds/emulates ionic cordova build/emulate android --prod.
But the 2nd way is only working on non-production builds ionic cordova build/emulate android.
But both codes work same in browser(s).

I can elaborate more in details what I am trying to achieve and how I have arranged my codes to do so. But that will be very long question.

Ionic info details -

@ionic/cli-utils  : 1.10.2
ionic (Ionic CLI) : 3.10.3

global packages:

    Cordova CLI : not installed

local packages:

    @ionic/app-scripts : 2.1.3
    Cordova Platforms  : android 6.2.3 ios 4.4.0
    Ionic Framework    : ionic-angular 3.6.0

System:

    Android SDK Tools : 26.0.2
    Node              : v8.1.3
    npm               : 5.3.0
    OS                : Windows 10

#2

I see no difference, although I generally would recommend preferring one of the Ionic lifecycle events (such as ionViewDidLoad) over ngOnInit.


#3

I tried using ionicViewDidLoad() like below -

ionicViewDidLoad(){
    this.events.subscribe('update-menu-title', (userInfo) => { // listener for "update-menu-title" event
        // Usage of 'userInfo'
    })
}

In this case, the custom event listener didn’t even registered itself. While other functionality worked normally.

Also, as I mentioned before, does different types of builds(minified/non-minified) causes this types of event listeners to behave differently?

Adding Ionic info details to question in case needed.


#4

It’s ionViewDidLoad, not ionic.


#5

I am extremely sorry for the spelling mistake.
I meant to type ionViewDidLoad() which I got to know form this document.

ionViewDidLoad(){
    this.events.subscribe('update-menu-title', (userInfo) => { // listener for "update-menu-title" event
        // Usage of 'userInfo'
    })
}

Also, I found out the Ionic page life cycle events will not trigger from app.component.ts because it is not a normal page. Only Angular life cycle hooks will work.
This is explained in this forum question and in this reported issue.

But, I am still unable to understand why optimized build ionic cordova build/emulate android --prod is not trigerring custom event listener if I modify my code as mentioned in point 2 in original question.