Ionic 3 iOS build --prod Not Working: Declarations of 2 Modules Error

只愿长相守 提交于 2019-12-11 06:39:36

问题


I have ruthlessly tried to build my Ionic 3 app for production using several different methods.

Attempts

Firstly I imported enableProdMode from Angular and called enableProdMode in my app.component.ts constructor. In turn, I got a blank screen when I built the app.

I decided that this was an unnecessary step, as the Ionic Framework documentation suggested that I simply run ionic cordova build ios --prod to get a production build. In turn, I got the following errors.

Errors

After running the aforementioned line of code to build my app for production, I received this in the console output and my build was then terminated (condensed for legibility).

Type AvatarComponent in .../avatar.ts is part of the declarations of 2 modules: AppModule in .../app.module.ts and AvatarComponentModule in .../avatar.module.ts! Please consider moving AvatarComponent in .../avatar.ts to a higher module that imports AppModule in .../app.module.ts and AvatarComponentModule in .../avatar.module.ts.

Thus, I tried doing exactly what Cordova prompted me to do, so I removed my avatar.module.ts file and voila, the error was gone. To my dismay, this issue occurred for every single component in my project (keep in mind, every component was generated automatically through ionic g component). After repeating this process for every component, my app began loading with a blank screen just as it had done when I tried using enableProdMode.

Misc. Details

cli packages:

    @ionic/cli-utils  : 1.13.1
    ionic (Ionic CLI) : 3.13.2

global packages:

    cordova (Cordova CLI) : 7.0.1 

local packages:

    @ionic/app-scripts : 1.3.7
    Cordova Platforms  : android 6.2.3 ios 4.4.0
    Ionic Framework    : ionic-angular 3.2.1

System:

    Android SDK Tools : 26.0.1
    Node              : v8.6.0
    npm               : 5.4.2 
    OS                : macOS Sierra
    Xcode             : Xcode 8.3.3 Build version 8E3004b 

Misc:

    backend : legacy

Summary

I would like a way to mitigate the Type ___ in ___/__.ts is part of the declarations of 2 modules issue, as well as an explanation as to why this occurs despite my usage of the pre-installed generation method.

Feel free to ask for clarification. Thank you so much in advance!


回答1:


Explanation for 'is part of the declarations of 2 modules'

To explain the ionic g issue, in ionic 3 they have started configuring your components to be lazy-loaded. this is why you are ending up with the .module.ts file. In my case I took a different approach, I actually included the module for the component in my app.module.ts

share.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { SharePage } from './share';

@NgModule({
  declarations: [
    SharePage,
  ],
  imports: [
    IonicPageModule.forChild(SharePage),
  ],
})
export class SharePageModule {}

app.module.ts

// I removed other modules just look at how I encorporate the component "SharedPageModule"
import {SharePageModule} from "../pages/share/share.module";

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    ComponentsModule,
    ProjectPageModule,
    SharePageModule,
    FormsModule,
    IonicModule.forRoot(MyApp,{
      preloadModules: true
    }),
    IonicStorageModule.forRoot(),
    MomentModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    EmailComposer,
    File,
    Globalization,
    Keyboard,
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    ModelProvider
  ]
})
export class AppModule {
}

Resolving your issue

I don't get any issues building with ionic cordova build ios --prod or white screen of death at run-time so perhaps it would help to import the modules as I have.

I doubt that will resolve your white screen of death so I would suggest debugging on your device or simulator.




回答2:


In my case, the solution was to delete the .module.ts (ngModule file) from each component and page folder generated by Ionic. Then, I made sure the components from my app were listed as declarations in my app.module.ts file. The app compiled perfectly in production.

Here is how my folder structure for a sample component or page looks like now:

/examplecomponent
    /examplecomponent.html
    /examplecomponent.scss
    /examplecomponent.ts

Additionally, here is how my app.module.ts file imports these components:

import { ExampleComponent } from '../components/examplecomponent/examplecomponent';

@NgModule({
      declarations: [
            ExampleComponent
      ],
      imports: [
            IonicModule.forRoot(App),
            CloudModule.forRoot(cloudSettings),
            ElasticModule,
            BrowserModule,
            HttpModule
      ],
      bootstrap: [IonicApp],
      entryComponents: [
            MyApp,
            MainPage
      ],
      providers: [
            {provide: ErrorHandler, useClass: IonicErrorHandler},
            // All Injectable Classes Go Here
            Alert,
            Analytics,
            Auth,
            Background
      ]
})
export class AppModule {}

If your issue persists, see Philip Brack's Solution.



来源:https://stackoverflow.com/questions/46840108/ionic-3-ios-build-prod-not-working-declarations-of-2-modules-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!