【问题标题】:Ionic App Error: StaticInjectorError(AppModule)[Content -> NavController]:离子应用程序错误:StaticInjectorError(AppModule)[Content -> NavController]:
【发布时间】:2018-04-06 13:38:29
【问题描述】:

在我的 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();
    }
}

仍然出现错误:

有什么帮助吗?

【问题讨论】:

    标签: angular ionic-framework ionic2 ionic3


    【解决方案1】:

    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 中设置我的离子菜单。我猜它在不同的组件上并不会改变它仍然是根级组件的事实。

    【讨论】:

    • 嗨,安吉尔。对不起..我真的很想了解...我创建了一个服务/提供者并尝试将其注入构造函数...得到了类似的错误。我仍然不明白是什么问题以及如何纠正它:/
    • 您是否将您的提供者(可注入)添加到 app.module.ts 中的提供者列表中?
    • 您能分享一下您的自定义提供程序和 app.module.ts 的代码吗?这个问题只涉及为什么你不能将 NavController 注入到你的菜单组件中。在相关发布的 app.module.ts 中,没有列出用户创建的提供程序。
    • 我的 app.module 已经在问题中,但我将使用提供程序添加和自定义提供程序进行编辑:D
    • 你想在哪里注入提供者?您发布的内容似乎一切正常。
    【解决方案2】:

    试试:

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

    它对我有用。

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2018-08-18
      • 2018-12-18
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多