首页 > 解决方案 > 离子 navCtrl.setPages 在 app.component.ts 中不起作用

问题描述

在我的 app.component.ts 文件中,如果用户点击 FCM 通知,我有一个代码会将用户发送到不同的页面。代码如下所示:

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);
}

当我执行通知时,我在 Xcode 编辑器中看到以下错误:

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

有人知道为什么吗?

标签: angularionic-frameworkionic3

解决方案


这是相关文档

如果您想从根应用程序组件控制导航怎么办?您不能注入NavController,因为作为导航控制器的任何组件都是根组件的子组件,因此它们不能被注入。

但是你可以使用导航

import { Nav } from 'ionic-angular';

//....

export class MyApp {

@ViewChild(Nav) nav: Nav;

//...

//And inside your function 

//..

this.nav.push('Page1');

//..

推荐阅读