离子应用程序错误:StaticInjectorError(AppModule)[内容 - > NavController]:

时间:2018-04-06 13:38:30

标签: angular ionic-framework ionic2 ionic3

我在constructor组件上添加任何内容时收到错误。

我制作了menu的一个组件,这是我的app.html:

<menu-component [content]="content"></menu-component>

<ion-nav #content [root]="rootPage"></ion-nav> 
<!-- I have to maintain this line here and on the MenuComponent cause the app didn't load the content without it... Don't know why-->

这是我的MenuComponent:

<ion-menu [content]="content">

    <ion-content>
        <ion-list>
            <button menuClose ion-item *ngFor="let page of pages" (click)="openPage(page)">
                <ion-icon name="{{page.icon}}"></ion-icon>
                {{page.title}}
            </button>
        </ion-list>
    </ion-content>

</ion-menu>

<ion-nav #content [root]="rootPage"></ion-nav>
<!-- See same line of app.html -->

这是我的MenuComponent.ts:

import { Component, Input } from '@angular/core';
import { NavController } from 'ionic-angular';

/* PAGES */
import { HomePage } from '../../../pages/home/home';
import { ContactPage } from '../../../pages/contact/contact';

@Component({
    selector: 'menu-component',
    templateUrl: 'menu.component.html'
})
export class MenuComponent {

    @Input() content;

    pages: Array<{ title: string, component: any, icon: string }>;

//This doesn't work
//    constructor( private navCtrl: NavController ) {

//This does
    constructor( ) {

        this.pages = [
            { title: 'HOME', component: HomePage, icon: 'md-home' },
            { title: 'CONTACT', component: ContactPage, icon: 'ios-chatbubbles' }
        ];

    }

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

也尝试了这种变化:

private navCtrl: NavController;

    constructor(navCtrl: NavController) {

        this.navCtrl = navCtrl;

最后这是我的app.module:

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';

/* PAGES */
import { HomePage } from '../pages/home/home';
import { ContactPage } from '../pages/contact/contact';

/* COMPONENTS */
import { MenuComponent } from '../shared/components/menu/menu.component';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    ContactPage,
    MenuComponent
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    ContactPage,
    MenuComponent
  ],
  providers: [
    StatusBar,
    SplashScreen,
    ModalUtil,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

提供商:ModalUtil.ts

import { Injectable } from "@angular/core";
import { ViewController, ModalController} from 'ionic-angular';

@Injectable()
export class ModalUtil {

    constructor( 
        private modalCtrl: ModalController,
        private viewCtrl: ViewController) {

    }

    presentModal(page: any, params?: Object) {

        let modal = this.modalCtrl.create(page.component, params);
        modal.present();
    }

    dismissModal(): void {
        this.viewCtrl.dismiss();
    }
}

仍然收到错误:

enter image description here

任何帮助人员?

2 个答案:

答案 0 :(得分:2)

If a MenuToggle button is added to the Navbar of a page, the button will only appear when the page it's in is currently a root page. The root page is the initial page loaded in the app, or a page that has been set as the root using the setRoot method on the NavController.

这是一种离子怪癖。您无法在根级别注入NavController。尝试在app.component.ts中注入NavController。您会看到类似/相同的错误。我通常在app.component.ts中设置我的离子菜单。我猜它是在一个不同的组件上并没有改变它仍然是根级组件的事实。

答案 1 :(得分:0)

尝试:

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

对我有用。