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

Ste*_*tro 100 declaration build ionic2 ionic3 angular

我尝试构建一个离子2应用程序.当我在浏览器中使用离子服务器尝试应用程序或在模拟器上启动它时,一切正常.

但是当我每次尝试构建错误时

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 creat a new NgModule that exports and includes AddEvent then import that NgModule in AppModule and AddEventModule
Run Code Online (Sandbox Code Playgroud)

我的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 {}
Run Code Online (Sandbox Code Playgroud)

和我的add-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 {}
Run Code Online (Sandbox Code Playgroud)

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

如果我从AppModule中删除声明,我会在运行离子服务时遇到错误,即找不到登录页面

sno*_*ete 171

从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 {}
Run Code Online (Sandbox Code Playgroud)

也,

请注意,如果要在该模块外部使用AddEvent组件,则AddEventModule导出AddEvent组件非常重要.幸运的是,您已经配置了,但是如果省略了,如果您尝试在AppModule的另一个组件中使用AddEvent组件,则会出现错误


Tom*_*kel 36

一些使用的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 { }
Run Code Online (Sandbox Code Playgroud)

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

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

@NgModule({
   imports: [
       SharedModule
       ....
       ....
 ]
})
export class WhateverModule { }
Run Code Online (Sandbox Code Playgroud)

  • 太棒了!我有一个补充...你需要将IonicPageModule.forChild(SharedModule)添加到ShareModule的Imports.在我这样做之前,我遇到了各种奇怪的问题. (5认同)

EmR*_*228 15

解决方案非常简单.查找*.module.ts文件和注释声明.在您的情况下查找addevent.module.ts文件并删除/注释下面的一行:

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

这个解决方案在离子3中用于离子cli生成的页面,并在两者ionic serveionic cordova build android --prod --release命令中工作!

要开心...


gng*_*ath 5

IN Angular 4.此错误被视为ng服务运行时缓存问题.

case:1一旦你在一个模块中导入组件并再次导入子模块,就会发生这个错误.

case:2将一个组件导入错误的位置并在正确的模块中删除并更换,那时它将被视为ng服务缓存问题.需要停止项目并再次启动-do服务,它将按预期工作.


小智 5

如果您的页面是使用CLI创建的,那么它会创建一个带有filename.module.ts的文件,然后您必须在app.module.ts文件中的imports数组中注册您的filename.module.ts,并且不要在声明数组中插入该页面.

例如.

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,
],
Run Code Online (Sandbox Code Playgroud)