我正在开发一个通用的Ionic 2应用程序,它包括在第一页上加载和显示多个组件。
每个附加组件都需要在应用程序中轻松创建和实现。所以我创建了一个名为" addon-declaration.ts "的文件。在这个文件中,我导出了所有组件:
export { MyFirstAddon } from './components/addon1/first.addon';
export { MySecondAddon } from './components/addon2/second.addon';
export { MyThirdAddon } from './components/addon3/third.addon';
所以我的问题是如何直接导入我的所有组件" app.module.ts "声明字段?
我已经尝试了这个,但它没有用:/
import * as ModuleHandler from '../models/addons/addon-declaration';
@NgModule({
declarations: [
MyApp,
ModuleHandler <--- Not working
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}
如果我逐个导入它们,它会很好用:
import { MyFirstAddon } from'../components/addon1/first.addon';
import { MySecondAddon } from'../components/addon2/second.addon';
import { MyThirdAddon } from'../components/addon3/third.addon';
@NgModule({
declarations: [
MyApp,
MyFirstAddon, <--- Working well
MySecondAddon,
MyThirdAddon
],
答案 0 :(得分:4)
创建一个导出组件和指令的功能模块
@NgModule({
declarations: [MyFirstAddon, MySecondAddon, MyThirdAddon],
exports: [MyFirstAddon, MySecondAddon, MyThirdAddon]
})
export class MyAddonModule {}
将该模块添加到要使用插件的其他模块的导入
import { MyAddonModule } from './components/my-addon-module';
@NgModule({
declarations: [
MyApp,
ModuleHandler <--- Not working
],
imports: [
IonicModule.forRoot(MyApp), MyAddonModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
答案 1 :(得分:1)
在放置所有插件文件的文件夹中创建一个index.ts
,然后导出所有文件,如:
export * from './first.addon';
export * from './second.addon';
export * from './third.addon';
.....
然后您需要导入app.module.ts
中的所有插件文件,如:
import * as allAddons from './addons_files';
最后你需要使用allAddons
对象,如:
@NgModule({
declarations: [
// all-addons
allAddons.FirstAddon,
allAddons.SecondAddon,
allAddons.ThirdAddon
]
})
export class AppModule() {
}
答案 2 :(得分:0)
declarations: [
MyApp,
ModuleHandler.MyFirstAddon,
ModuleHandler.MySecondAddon,
ModuleHandler.MyThirdAddon
]
或使用导出它们并导入模块的ngModule。