简体   繁体   English

Ionic 3从app.component类重定向到另一个页面

[英]Ionic 3 redirect from app.component class to another page

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 doesn't work here, so I was wondering what will? NavController在这里不起作用,所以我想知道会怎样?

export class MyApp{

    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);
                // I want to redirect to another page with msg object instead of HomePage
            });

    }
}

Because in app.component.ts under MyApp{}, when I declare constructor(public navCtrl:nacNavController) then I get the following error: 因为在MyApp {}下的app.component.ts中,当我声明constructor(public navCtrl:nacNavController) ,出现以下错误:

Error: Uncaught (in promise): Error: No provider for NavController!
Error: No provider for NavController!
    at injectionError (main.js:1509)
    at noProviderError (main.js:1547)
    at ReflectiveInjector_._throwOrNull (main.js:3048)
    at ReflectiveInjector_._getByKeyDefault (main.js:3087)
    at ReflectiveInjector_._getByKey (main.js:3019)
    at ReflectiveInjector_.get (main.js:2888)
    at AppModuleInjector.NgModuleInjector.get (main.js:3835)
    at resolveDep (main.js:11202)
    at createClass (main.js:11071)
    at createDirectiveInstance (main.js:10899)
    at injectionError (main.js:1509)
    at noProviderError (main.js:1547)
    at ReflectiveInjector_._throwOrNull (main.js:3048)
    at ReflectiveInjector_._getByKeyDefault (main.js:3087)
    at ReflectiveInjector_._getByKey (main.js:3019)
    at ReflectiveInjector_.get (main.js:2888)
    at AppModuleInjector.NgModuleInjector.get (main.js:3835)
    at resolveDep (main.js:11202)
    at createClass (main.js:11071)
    at createDirectiveInstance (main.js:10899)
    at c (polyfills.js:3)
    at polyfills.js:3
    at polyfills.js:3
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at <anonymous>

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. 您不能注入NavController因为作为导航控制器的任何组件都是根组件的子代,因此无法注入它们。

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 ): 通过向ion-nav添加参考变量,可以使用@ViewChild来获取Nav组件的实例,该实例是导航控制器(它扩展了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): 因此,在您的情况下,您只需要检查一下app.component.html文件是否包含此文件(请注意#myNav模板变量):

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

    }
}

I don't see any reason why you can't use the NavController like so: 我看不出为什么不能像这样使用NavController任何原因:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM