gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:51 25 4
gpt4 key购买 nike

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

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>

最佳答案

你应该 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);
}
}

所以在你的情况下,你只需要检查你的 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! :)
});

}
}

关于angular - Ionic 3 从 app.component 类重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com