[英]How Can I create a wrapper module in Angular2 for multiple modules?
I am currently trying to clean up my project structure and I wanted to keep my AppComponent as clean as possible. 我目前正在尝试清理项目结构,并且希望保持AppComponent尽可能干净。 I am importing angular2 material in its own file:
我在自己的文件中导入angular2材料:
import { NgModule } from '@angular/core';
// Material
import { MdCardModule } from '@angular2-material/card';
import { MdButtonModule } from '@angular2-material/button';
import { MdInputModule } from '@angular2-material/input';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdListModule } from '@angular2-material/list';
import { MdIconModule, MdIconRegistry } from '@angular2-material/icon';
export const MATERIAL_UI_MODULES = [
MdCardModule,
MdButtonModule,
MdInputModule,
MdToolbarModule,
MdIconModule,
MdListModule
]
export const MATERIAL_UI_REGISTRIES = [
MdIconRegistry
]
and inside my AppController I am using it as follows: 在我的AppController中,我使用它的方式如下:
const APP_PROVIDERS = [
...MATERIAL_UI_REGISTRIES
];
const APP_MODULES = [
...MATERIAL_UI_MODULES,
];
@NgModule({
imports:APP_MODULES,
declarations: APP_DECLARATIONS,
bootstrap:[AppComponent],
providers: APP_PROVIDERS,
})
export class AppModule {
}
but is there a way to create a MaterialModule
that I can just import and not have to worry about adding individual modules or providers. 但是有一种方法可以创建我可以导入的
MaterialModule
,而不必担心添加单个模块或提供程序。
I have tried creating a MaterialModule: 我尝试创建MaterialModule:
@NgModule({
imports:[
...MATERIAL_UI_MODULES,
],
providers: MATERIAL_UI_REGISTRIES,
})
export class MaterialModule {
}
and importing in my AppComponent: 并导入我的AppComponent:
@NgModule({
imports:[
//...MATERIAL_UI_MODULES,
MaterialModule,
FormsModule,
BrowserModule,
routing,
ComponentsModule
],
declarations: APP_DECLARATIONS,
bootstrap:[AppComponent],
providers: APP_PROVIDERS,
})
export class AppModule {
}
But I get the following error 但我收到以下错误
zone.js:355 Unhandled Promise rejection: Template parse errors:
'md-card-title' is not a known element:
1. If 'md-card-title' is an Angular component, then verify that it is part of this module.
2. If 'md-card-title' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to
suppress this message. ("....
You can use SharedModule . 您可以使用SharedModule 。 Declare common things in SharedModule and then import SharedModule in components where you want to use common things.
在SharedModule中声明普通的东西,然后将SharedModule导入要使用普通东西的组件中。
Learn more about SharedModule here : https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module 在此处了解有关SharedModule的更多信息: https ://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.