Component is part of the declaration of 2 modules

前端 未结 16 756
我在风中等你
我在风中等你 2020-12-04 06:44

I try to build an ionic 2 app. When I try the app in the browser with ionic serve or launch it on an emulator everything works fine.

But when I try to build it every

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 07:16

    To surpass this error , you should start by removing all the imports of app.module.ts and have something like this :

                import { BrowserModule } from '@angular/platform-browser';
                import { HttpClientModule } from '@angular/common/http';
                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';
    
    
                @NgModule({
                  declarations: [
                    MyApp
                  ],
                  imports: [
                    BrowserModule,
                    HttpClientModule,
                    IonicModule.forRoot(MyApp)
                  ],
                  bootstrap: [IonicApp],
                  entryComponents: [
                    MyApp
                  ],
                  providers: [
                    StatusBar,
                    SplashScreen,
                    {provide: ErrorHandler, useClass: IonicErrorHandler}
                  ]
                })
                export class AppModule {}
    

    Next edit your pages module , like this :

                import { NgModule } from '@angular/core';
                import { IonicPageModule } from 'ionic-angular';
                import { HomePage } from './home';
    
                @NgModule({
                  declarations: [
                    HomePage,
                  ],
                  imports: [
                    IonicPageModule.forChild(HomePage),
                  ],
                  entryComponents: [HomePage]
                })
                export class HomePageModule {}
    

    Then add annotation of IonicPage before component annotation of all the pages :

    import { IonicPage } from 'ionic-angular';
    
    @IonicPage()
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    

    Then edit your rootPage type to be string and remove the imports of pages (if there is any in your app component )

    rootPage: string = 'HomePage';
    

    Then the navigation function should be like this :

     /**
    * Allow navigation to the HomePage for creating a new entry
    *
    * @public
    * @method viewHome
    * @return {None}
    */
     viewHome() : void
     {
      this.navCtrl.push('HomePage');
     }
    

    You can find the source of this solution here : Component is part of the declaration of 2 modules

提交回复
热议问题