Angular Ionic navCtrl.setPages在app.component.ts中不工作
在我的app.component.ts文件中,我有一个代码,如果用户点击FCM通知,它会将用户发送到另一个页面 代码如下所示:Angular Ionic navCtrl.setPages在app.component.ts中不工作,angular,ionic-framework,ionic3,Angular,Ionic Framework,Ionic3,在我的app.component.ts文件中,我有一个代码,如果用户点击FCM通知,它会将用户发送到另一个页面 代码如下所示: this.fcm.listenToNotifications().subscribe(async(notification) => { if (notification.tap) { const pages = [ {page: Page1}, {page: Page
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
2019-01-17 16:55:13.230990+0200adservio[2103:800067]错误:未处理的承诺拒绝:null不是对象(评估'this.navCtrl.setPages');区域:;任务:承诺;Value:TypeError:null不是对象(计算'this.navCtrl.setPages')http://localhost:8080/var/containers/Bundle/Application/9301607C-7904-4404-B00D-FE8D9E7EED17/Adservio.app/www/build/main.js:1:1380986
有人知道为什么吗?在根组件中,应该使用Nav而不是NavController。请参阅此处的更多信息: 无法注入NavController,因为作为导航控制器的任何组件都是根组件的子组件,因此它们无法被注入 同时检查参考应用程序实现: 这里是相关的: 如果您想控制根应用程序组件的导航,该怎么办? 无法注入NavController,因为任何组件 导航控制器是根组件的子组件,因此 无法进行注射 但是您可以使用Nav
import { Nav } from 'ionic-angular';
//....
export class MyApp {
@ViewChild(Nav) nav: Nav;
//...
//And inside your function
//..
this.nav.push('Page1');
//..
删除
async
,subscribe已在为您处理该问题。