【发布时间】:2018-05-04 01:52:57
【问题描述】:
Ionic/Angular 开发的新手,我有以下结构
- src
-- /app
---- app.component.ts
---- app.module.ts
---- main.ts
---- ...
-- /pages
---- /event-home
------ /event-home.module.ts
------ /event-home.ts
event-home.module.ts:
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { EventHomePage } from './event-home';
@NgModule({
declarations: [
EventHomePage,
],
imports: [
IonicPageModule.forChild(EventHomePage),
],
entryComponents: [
EventHomePage
]
})
export class EventHomePageModule {}
app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { EventEntryPage, EventHomePage, EventDiscussionPage,
EventHandoutsPage, EventInfoPage } from "../pages/pages";
@NgModule({
declarations: [
MyApp,
EventEntryPage,
EventHomePage,
EventDiscussionPage,
EventHandoutsPage,
EventInfoPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
EventEntryPage,
EventHomePage,
EventDiscussionPage,
EventHandoutsPage,
EventInfoPage
],
providers: [
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
我正在尝试创建到 http://localhost:8100/#/event-home 的深层链接,在 event-home.ts 中我使用 IonicPage()。
当我访问该网址时,我得到Error: Type EventHomePage is part of the declarations of 2 modules: AppModule and EventHomePageModule! Please consider moving EventHomePage to a higher module that imports AppModule and EventHomePageModule.
我认为我需要创建一个共享模块,但我仍在努力思考如何做到这一点。我从以下内容开始:
import {NgModule} from "@angular/core";
import {EventHomePage} from "../pages/event-home/event-home";
@NgModule({
declarations: [ EventHomePage ],
exports: [ EventHomePage ]
})
export class SharedModule {}
但我被困在“我现在该怎么办?”。如果还不清楚,我在这里有点盲目,所以如果我追求错误的解决方案,请指出。
【问题讨论】:
-
只需从您的
app.module.ts中删除EventHomePage -
这样做会导致
Component EventHomePage is not part of any NgModule or the module has not been imported into your module. -
您还保留
event-home.module.ts文件吗? -
文件仍然存在,是的。如果我删除它 Ionic 抱怨在 event-home.ts 中使用 IonicPage() 而没有 event-home.module.ts
-
确保只在
event-home.module.ts和event-home.module.ts中导入EventHomePage,不要使用entryComponents。更简单的方法是使用Ionic CLI:ionic g page event-home。您将看到它是如何在模块中导入的。
标签: angular ionic-framework ionic3