我尝试构建一个 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 {}
而我的 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 {}
我知道我必须删除其中一个声明,但我不知道如何。
如果我从 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
组件,则会出现错误/p>
关于angular - 组件是2个模块声明的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598311/