组件是 2 个模块声明的一部分

Component is part of the declaration of 2 modules

我尝试构建一个 ionic 2 应用程序。 当我在带有 ionic serve 的浏览器中尝试该应用程序或在模拟器上启动它时,一切正常。

但是当我尝试构建它时,每次都出错

ionic-app-script tast: "build"
Error Type AddEvent in "PATH"/add.event.ts is part of the declarations of 2 modules: AppModule in "PATH"/app.modules.ts and AddEvent in "PATH"/add-event.module.ts.
Please consider moving AddEvent in "PATH"/add-event.ts to a higher module that imports AppModule in "PATH"/app.module.ts and AddEventModule.
You can also create a new NgModule that exports and includes AddEvent then import that NgModule in AppModule and AddEventModule

我的 AppModule 是

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { AngularFireModule } from 'angularfire2';
import { MyApp } from './app.component';
import { Eventdata } from '../providers/eventdata';
import { AuthProvider } from '../providers/auth-provider';
import { HttpModule } from '@angular/http';

import { HomePage } from '../pages/home/home';
import { Login } from '../pages/login/login';
import { Register } from '../pages/register/register';
import { AddEvent } from '../pages/add-event/add-event';
import { EventDetails } from '../pages/event-details/event-details';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
   

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    Login,
    Register,
    AddEvent,
    EventDetails
    
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpModule,
    AngularFireModule.initializeApp(config)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    Login,
    Register,
    AddEvent,
    EventDetails
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}, Eventdata, AuthProvider
  ]
})
export class AppModule {}

我的添加-event.module.ts是

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { AddEvent } from './add-event';

@NgModule({
  declarations: [
    AddEvent,
  ],
  imports: [
    IonicPageModule.forChild(AddEvent),
  ],
  exports: [
    AddEvent
  ]
})
export class AddEventModule {}

我知道我必须删除其中一项声明,但我不知道如何。

如果我从 AppModule 中删除声明,我会收到一个错误,提示找不到登录页面,而 运行 ionic serve

AppModule 中删除声明,但更新 AppModule 配置以导入您的 AddEventModule

.....
import { AddEventModule } from './add-event.module';  // <-- don't forget to import the AddEventModule class

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    Login,
    Register,
    //AddEvent,  <--- remove this
    EventDetails

  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpModule,
    AngularFireModule.initializeApp(config),
    AddEventModule,  // <--- add this import here
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    Login,
    Register,
    AddEvent,
    EventDetails
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}, Eventdata, AuthProvider
  ]
})
export class AppModule {}

此外,

请注意,如果您想在该模块外使用 AddEventModule 组件,请务必导出 AddEvent 组件。幸运的是,您已经进行了配置,但如果省略它,那么如果您尝试在 AppModule

的另一个组件中使用 AddEvent 组件,就会出现错误

我遇到了同样的错误,但我发现当您导入 AddEventModule 时,您无法导入 AddEvent 模块,因为在这种情况下会出现错误。

此模块会在您 运行 ionic 命令时自动添加。不过没必要。因此,另一种解决方案是从项目中删除 add-event.module.ts

IN Angular 4. 这个错误被认为是 ng serve 运行 时间缓存问题。

case:1 会出现这个错误,一旦你在一个模块中导入组件,然后再次在子模块中导入就会发生。

case:2 在错误的地方导入一个组件并在正确的模块中删除和替换,此时它认为是 ng serve 缓存问题。需要停止项目并重新启动-do ng serve,它将按预期工作。

您可以试试这个解决方案(适用于 Ionic 3)

就我而言,当我使用以下代码调用页面时会发生此错误

 this.navCtrl.push("Login"); // Bug

我刚刚删除了如下引号,并将该页面导入到我用来调用登录页面的文件顶部

this.navCtrl.push(Login); // Correct

我现在无法解释其中的区别,因为我是初级开发人员

解决方法很简单。查找 *.module.ts 个文件和注释声明。在您的情况下,找到 addevent.module.ts 文件和 remove/comment 下面一行:

@NgModule({
  declarations: [
    // AddEvent, <-- Comment or Remove This Line
  ],
  imports: [
    IonicPageModule.forChild(AddEvent),
  ],
})

此解决方案在 ionic 3 中适用于由 ionic-cli 生成的页面,并且适用于 ionic serveionic cordova build android --prod --release 命令!

开心...

自从 Ionic 3.6.0 发布以来,使用 Ionic-CLI 生成的每个页面现在都是一个 Angular 模块。 这意味着您必须将模块添加到文件 src/app/app.module.ts

中的导入中
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";
import { HomePage } from "../pages/home/home"; // import the page
import {HomePageModule} from "../pages/home/home.module"; // import the module

@NgModule({
  declarations: [
    MyApp,
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HomePageModule // declare the module
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage, // declare the page
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: ErrorHandler, useClass: IonicErrorHandler },
  ]
})
export class AppModule {}

要克服此错误,您应该从删除 app.module.ts 的所有导入开始,并拥有类似这样的内容:

            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 {}

接下来编辑您的页面模块,像这样:

            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 {}

然后在所有页面的组件注解前加上IonicPage的注解:

import { IonicPage } from 'ionic-angular';

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

然后将您的 rootPage 类型编辑为字符串并删除页面的导入(如果您的应用程序组件中有)

rootPage: string = 'HomePage';

那么导航功能应该是这样的:

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

您可以在此处找到此解决方案的来源:Component is part of the declaration of 2 modules

如果您的页面是使用 CLI 创建的,那么它会创建一个包含 filename.module.ts 的文件,那么您必须注册您的 filename.module.ts in imports array in app.module.ts file and don't insert that page in declarations array.

例如

import { LoginPageModule } from '../login/login.module';


declarations: [
    MyApp,
    LoginPageModule,// remove this and add it below array i.e. imports
],

imports: [
        BrowserModule,
        HttpModule,
        IonicModule.forRoot(MyApp, {
           scrollPadding: false,
           scrollAssist: true,
           autoFocusAssist: false,
           tabsHideOnSubPages:false
        }),
       LoginPageModule,
],

简单修复,

转到您的 app.module.ts 文件和 remove/commentadd_event 绑定的所有内容。不需要向 ionic cli 生成的 App.module.t 添加组件,因为它为名为 components.module.ts 的组件创建了一个单独的模块。

它具有所需的模块组件导入

一些使用 Lazy loading 的人会无意中看到这个页面。

这是我修复共享 指令的方法。

  1. 创建一个新的共享模块

shared.module.ts

import { NgModule, Directive,OnInit, EventEmitter, Output, OnDestroy, Input,ElementRef,Renderer } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SortDirective } from './sort-directive';

@NgModule({
  imports: [
  ],
  declarations: [
  SortDirective
  ],
  exports: [
    SortDirective
  ]
})

export class SharedModule { }

然后在 app.module 和您的其他模块中

import {SharedModule} from '../directives/shared.module'
...

@NgModule({
   imports: [
       SharedModule
       ....
       ....
 ]
})
export class WhateverModule { }

解决了 -- 组件是 2 个模块声明的一部分

  1. 删除应用程序中的 pagename.module.ts 文件
  2. 从 'ionic-angular' 中删除导入 { IonicApp };在 pagename.ts 文件中
  3. 从 pagename.ts 文件中删除 @IonicPage()

和运行命令ionic cordova build android --prod --release 它在我的应用程序中工作

有同样的问题。只需确保删除 "declarations" 中出现的所有模块,但 AppModule.

对我有用。

如错误所述,如果您的 Page/Component 已经有离子模块文件,则从根目录中删除模块 AddEvent 如果不只是将其从 other/child page/component 中删除,最后page/component 如果要使用,应该只出现在一个导入的模块文件中。

具体来说,如果在多个页面中需要,则应添加根模块,如果在特定页面中,则仅在一个页面中添加。

我不小心创建了我自己的组件,其名称与库的组件相同。

当我使用 IDE 到 auto-import 组件库时,我选择了错误的库。

因此出现此错误,我是 re-declaring 组件。

我通过从我自己的组件代码而不是库中导入来修复。

我也可以通过不同的命名来解决:避免歧义。

在这种情况下,创建另一个共享模块,导入多个模块中使用的所有组件。

在共享组件中。声明那些组件。然后在 appmodule 以及您要访问的其他模块中导入共享模块。它会 100% 起作用,我这样做了并且让它起作用了。

@NgModule({
    declarations: [HeaderComponent, NavigatorComponent],
    imports: [
        CommonModule, BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        FormsModule,
    ] 
})
export class SharedmoduleModule { }
const routes: Routes = [
{
    path: 'Parent-child-relationship',
    children: [
         { path: '', component: HeaderComponent },
         { path: '', component: ParrentChildComponent }
    ]
}];
@NgModule({
    declarations: [ParrentChildComponent, HeaderComponent],
    imports: [ RouterModule.forRoot(routes), CommonModule, SharedmoduleModule ],
    exports: [RouterModule]
})
export class TutorialModule {
}
imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    FormsModule,
    MatInputModule,
    MatButtonModule,
    MatSelectModule,
    MatIconModule,
    SharedmoduleModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }