我创建了一个共享模块并在其他模块中声明并导出了我需要的组件。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DateslideComponent } from './dateslide/dateslide.component';
import { IonicModule } from '@ionic/angular';
import { TimeslideComponent } from './timeslide/timeslide.component';
import { AddtimeComponent } from './addtime/addtime.component'
@NgModule({
declarations: [DateslideComponent, TimeslideComponent, AddtimeComponent],
imports: [
CommonModule,
IonicModule
],
exports: [DateslideComponent, TimeslideComponent, AddtimeComponent]
})
export class TimeModule { }
在另一个模块中,我导入了共享模块。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { WhenPageRoutingModule } from './when-routing.module';
import { WhenPage } from './when.page';
import {TimeModule} from '../../timemodule/time.module';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
WhenPageRoutingModule,
TimeModule
],
declarations: [WhenPage ]
})
export class WhenPageModule {}
在另一个模块的一个组件中,我从共享模块导入了该组件,但我收到以下错误
import { AddtimeComponent } from '../../timemodule/time.module'
module declares component locally, but it is not exported.
最佳答案
您无需在主模块中导入 AddtimeComponent。您必须将其导出到 SharedModule 中,如下所示。
import { AddtimeComponent } from './addtime/addtime.component';
export { AddtimeComponent } from './addtime/addtime.component';
NgModule 装饰器中的导出属性和 header 中的导出是不同的。 NgModule 中的 export 属性用于 Angular 编译器,而 header 中的 export 用于 Typescript 编译器。如果你打算只使用选择器,那么在 NgModule 装饰器中提及就足够了。如果要在其他模块中导入 Typescript 类,则必须在功能模块中导出该类。
关于angular - module 在本地声明组件,但未导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60740708/