Ionic navCtrl.setPages not working in app.component.ts

Viewed 594

In my app.component.ts file I have a code that sends the user to a different page if he taps on a FCM notification The code is something like this:

this.fcm.listenToNotifications().subscribe(async(notification) => {             
    if (notification.tap) {
        const pages = [
            {page: Page1},
            {page: Page1Subpage, params: {...}}
        ];
    this.navCtrl.setPages(pages);
    this.navCtrl.parent.select(1);
}

When I execute the notification, I see the following error in my Xcode editor:

2019-01-17 16:55:13.230990+0200 Adservio[2103:800067] ERROR: Unhandled Promise rejection: null is not an object (evaluating 'this.navCtrl.setPages') ; Zone: <root> ; Task: Promise.then ; Value: TypeError: null is not an object (evaluating 'this.navCtrl.setPages') http://localhost:8080/var/containers/Bundle/Application/9301607C-7904-4404-B00D-FE8D9E7EED17/Adservio.app/www/build/main.js:1:1380986

Does anybody have any idea why ?

2 Answers

Here is related documentation :

What if you want to control navigation from your root app component? You can't inject NavController because any components that are navigation controllers are children of the root component so they aren't available to be injected.

But you can use Nav

import { Nav } from 'ionic-angular';

//....

export class MyApp {

@ViewChild(Nav) nav: Nav;

//...

//And inside your function 

//..

this.nav.push('Page1');

//..
Related