Ionic 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
有人知道为什么吗?
这里是相关的 文档 :
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.
但您可以使用 Nav
import { Nav } from 'ionic-angular';
//....
export class MyApp {
@ViewChild(Nav) nav: Nav;
//...
//And inside your function
//..
this.nav.push('Page1');
//..
在根组件中,您应该使用 Nav 而不是 NavController。更多信息请参见: https://ionicframework.com/docs/api/navigation/NavController/#navigating-from-the-root-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.
另请检查参考应用程序实现: https://github.com/ionic-team/ionic-conference-app/blob/v3/src/app/app.component.ts