我创建了一个共享模块,并在其他模块中声明并导出了我需要的组件。
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'
模块在本地声明组件,但不会导出。
答案 0 :(得分:5)
您不需要在主模块中导入AddtimeComponent。您必须将其导出到SharedModule中,如下所示。
import { AddtimeComponent } from './addtime/addtime.component';
export { AddtimeComponent } from './addtime/addtime.component';
NgModule装饰器中的export属性和标头中的export属性不同。 NgModule中的export属性用于Angular编译器,而header中的export属性用于Typescript编译器。
如果您打算仅使用选择器,则在NgModule装饰器中提及就足够了。如果要在其他模块中导入Typescript类,则必须在功能模块中导出该类。
答案 1 :(得分:2)
您无需再次导入组件,只需将SharedModule导入其他模块中,就可以使用在SharedModule中声明为&&导出的组件了。
import { SharedModule } from '/path/to/SharedModule';
@NgModule({
imports: [
...
SharedModule
]
})
请尝试在Stackblitz中复制此设置,或共享对存储库的访问权限,以便我们调试您的问题并为您提供解决方案。
编辑>>由于您正在尝试在模块的构建过程中引导组件。然后可能需要使用EntryComponent使组件本身从加载的模块的引导程序中提供。
在代码中查看它:
import { SharedModule } from '/path/to/SharedModule';
import { AddtimeComponent } from '/path/to/AddtimeComponent';
@NgModule({
imports: [
...
SharedModule
],
entryComponents: [
AddtimeComponent
]
})
有关更多信息,请结帐:https://codinglatte.com/posts/angular/entry-components-angular/