Ionic 3 redirect from app.component class to another page

前端 未结 2 1021
臣服心动
臣服心动 2021-01-22 04:11

I’m getting push notification messages and once I receive the message, I want to redirect to another page or show another page instead of home page.

NavController<

相关标签:
2条回答
  • 2021-01-22 04:44

    You should read the docs...

    Navigating from the Root component

    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.

    By adding a reference variable to the ion-nav, you can use @ViewChild to get an instance of the Nav component, which is a navigation controller (it extends NavController):

    import { Component, ViewChild } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    @Component({
       template: '<ion-nav #myNav [root]="rootPage"></ion-nav>'
    })
    export class MyApp {
       @ViewChild('myNav') nav: NavController
       public rootPage: any = TabsPage;
    
       // Wait for the components in MyApp's template to be initialized
       // In this case, we are waiting for the Nav with reference variable of "#myNav"
       ngOnInit() {
          // Let's navigate from TabsPage to Page1
          this.nav.push(Page1);
       }
    }
    

    So in your case, you just need to check if your app.component.html file includes this (please notice the #myNav template variable):

    <ion-nav #myNav [root]="rootPage"></ion-nav>
    

    And then in the component code:

    import { Component, ViewChild } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    //...
    export class MyApp{
        @ViewChild('myNav') nav: NavController
        rootPage: any = HomePage;
    
        constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public push: Push) {
    
            platform.ready().then(() => {
                // Okay, so the platform is ready and our plugins are available.
                // Here you can do any higher level native things you might need.
                statusBar.styleDefault();
                splashScreen.hide();
            });
    
    
            this.push.rx.notification()
                .subscribe((msg) => {
                    alert(msg.title + ': ' + msg.text);
                    this.nav.push(TheOtherPage); // <- use it here! :)
                });
    
        }
    }
    
    0 讨论(0)
  • 2021-01-22 04:47

    I don't see any reason why you can't use the NavController like so:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { Push, PushToken } from '@ionic/cloud-angular';
    import { SomeOtherPage } from '...';
    
    export class HomePage {
    
      constructor(private push: Push
        public navCtrl: NavController) {
            push.register().then((t: PushToken) => {
              return this.push.saveToken(t);
            }).then((t: PushToken) => {
              console.log('Token saved:', t.token);
            });
            this.push.rx.notification().subscribe((msg) => {
              this.navCtrl.push(SomeOtherPage, { msg: msg });
            });
      }
    }
    
    0 讨论(0)
提交回复
热议问题