gpt4 book ai didi

angular - ionic 2 : EXCEPTION: No provider for NavController

转载 作者:太空狗 更新时间:2023-10-29 16:57:43 26 4
gpt4 key购买 nike

我的 ionic 2/angular 2 应用有问题。

我有一个 app.ts,其中的漏洞“auth”部分是 implementet。

代码如下:

 import {Nav, Platform, Modal, ionicBootstrap} from "ionic-angular";
import {NavController} from "ionic-angular/index";
import {StatusBar} from "ionic-native";
import {Component, ViewChild} from "@angular/core";
import {AngularFire, FirebaseListObservable, FIREBASE_PROVIDERS, defaultFirebase} from "angularfire2";
import {HomePage} from "./pages/home/home";
import {AuthPage} from "./pages/auth/home/home";

@Component({
templateUrl: "build/app.html",
})

class MyApp {
@ViewChild(Nav) nav: Nav;

authInfo: any;
rootPage: any = HomePage;
pages: Array<{title: string, component: any}>;

constructor(private platform: Platform, private navCtrl: NavController, private af: AngularFire) {
this.initializeApp();

this.pages = [
{ title: "Home", component: HomePage }
];

}

initializeApp() {
this.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();
});
}

openPage(page) {
this.nav.setRoot(page.component);
}

ngOnInit() {
this.af.auth.subscribe(data => {
if (data) {
this.authInfo = data;
} else {
this.authInfo = null;
this.showLoginModal();
}
});
}

logout() {
if (this.authInfo) {
this.af.auth.logout();
return;
}
}

showLoginModal() {
let loginPage = Modal.create(AuthPage);
this.navCtrl.present(loginPage);
}
}

但是现在,当我尝试运行该应用程序时,我收到了这条消息:

ORIGINAL EXCEPTION: No provider for NavController

你知道如何解决这个问题吗?谢谢!

最佳答案

您不能通过构造函数在 Root 组件中注入(inject) NavController

所以,基本上你可以做如下的事情:-

constructor(private nav: NavController){
}

这就是你如何注入(inject) NavController

@Controller({
....
})
class MyApp{
@ViewChild('nav') nav: NavController;
....
....
constructor(...){ // See, no NavController here
}
....
}

这就是 Ionic 文档必须要说的。

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)

关于angular - ionic 2 : EXCEPTION: No provider for NavController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38121961/

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