【问题标题】:Ionic 3 redirect from app.component class to another pageIonic 3 从 app.component 类重定向到另一个页面
【发布时间】:2017-11-27 23:51:59
【问题描述】:

我收到推送通知消息,收到消息后,我想重定向到另一个页面或显示另一个页面而不是主页。

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

    }
}

因为在 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>

【问题讨论】:

  • 你说的 NavController 在这里不起作用是什么意思
  • 我的意思是在 app.component.ts 中,当我使用 NavController 时出现错误 Error: Uncaught (in promise): Error: No provider for NavController!

标签: angular typescript ionic2 ionic3


【解决方案1】:

你应该read the docs...

从根组件导航

如果您想从根应用程序组件控制导航怎么办?您不能注入 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);
   }
}

所以在你的情况下,你只需要检查你的app.component.html 文件是否包含这个(请注意#myNav 模板变量):

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

然后在组件代码中:

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

    }
}

【讨论】:

  • 这应该被接受为答案。当然为我解决了。谢谢!
  • 很高兴听到它帮助了@Loke :)
【解决方案2】:

我看不出你为什么不能像这样使用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 });
        });
  }
}

【讨论】:

  • 你读过我的问题了吗,我提到MyApp {} 是app.component.ts 而不是HomePage{} 是home.ts。在 app.component.ts 中,当我使用 NavController 时出现错误 Error: Uncaught (in promise): Error: No provider for NavController!
  • 嘿阿卜杜勒,自从我读到这个问题后,你显然已经更新了这个问题,因为你最初只是说你不能使用 NavController,这没有意义,我以前没有遇到过你的问题,我会再仔细看看。
  • 为什么不直接把 push rx 订阅放到 Homepage 组件中,然后从那里进行路由?
猜你喜欢
  • 1970-01-01
  • 2017-09-11
  • 2016-12-14
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 2020-01-30
  • 2018-12-26
  • 1970-01-01
相关资源
最近更新 更多